what's going on everybody and welcome to
part 4 of the keavy tutorials in this
video what we're gonna be talking about
is scheduling things so up to this point
we've been talking about you know how
you can change screen and how you can
have button events and how a button
event could change the screen but
there's gonna be times where you just
simply want to control a changed screen
or other things in general so we know
how to use events to make things happen
now what we want to talk about is how to
just schedule a thing to happen either
one time or on some sort of cycle going
into the future so to do that we're
gonna use the keavy clock so it's just
from K V dot clock we're gonna import
clock now the other thing that we're
gonna go ahead and do is take the socket
code from the socket tutorial and just
put that in our directory here so I'm
gonna go ahead and make two new files
one is going to be socket client dot pi
yes and the other is going to be socket
server dot pi the code for both of these
is hosted on Python program Internet
I'll put links in the description if you
want to learn more about sockets again
go to the sockets tutorial because this
whole thing is all you know completely
explained so like socket server dot PI
all of this code is just a direct copy
and paste from the tutorial code from
there so I'm not going to go over that
twice so socket server dot PI I mean
open that pasta that and then the socket
client dot pi is pretty much the same
there's a few small changes but nothing
that should be confusing to anybody
that's gone through the series anyways
so I'm gonna paste in that and that's
what we're gonna use just for handling
the socket stuff because this is just a
Kimmy's tutorial where I'm just trying
to show the UI and how to do key v stuff
and I'm really trying to go over sockets
again so anyway cool so we've got that
stuff now we want to do is basically
when someone connects right now we do
this we show the info screen and then
after the info screen
is when we want to eventually change the
screen to the actual chat screen unless
we throw an error so we're gonna show
the info screen and then what we want to
do is we're going to say um
clock actually its capital C clock daunt
schedule
once and then what's the thing we want
to schedule and then like how how many
seconds in the future do we want to
schedule that so in this case that'll be
self dot Connect and we'll just do it
one second into the future
cool once we've done that we need to
well one we have to have this connect
method that would probably be somewhat
useful so just we're gonna add this
connect method literally on the connect
page so it's just a new method run or
neath or join button summer to say
define connects the instance and then
we're gonna use an underscore here the
underscore is basically any time you
schedule something it's gonna pass two
things it's gonna pass the instance but
it's also gonna pass how many seconds
has gone by transpired since the
schedule so in this case it'll be one
and it will always be one because we're
scheduling once but sometimes you're
gonna schedule on a cycle and it might
be useful to know how long it's been
since that scheduling okay so anyway
that gets passed we don't need it though
so underscore to the note it's useless
info to us now what we're going to do is
we're going to set up the values for
port IP and username so port is just
gonna be an integer of self dot port
text the IP will be self dot IP text and
the username will be self dot username
dot text and then we want to connect so
the first thing I'm going to do before
we do that is come over here and we're
going to import socket underscore client
and just for the record socket client
imports the server stuff so we don't
really need to do anything there and
actually really the server just runs so
we're going to import socket client
and that should run all the socket stuff
so we're never gonna really need to
touch the sockets other than for handle
error handling rather coming back down
here where was I connect so now what we
want to do is attempt to connect but
also handle for errors if for whatever
reason we don't connect so we're going
to say if not socket underscore client
dot connects IP port username and then
show error will be our error method here
so if not we're just going to simply
return so we actually we scheduled this
connection we return if nothing works so
we're done with the connect method and
basically show error we're gonna create
that in a minute what that's going to do
is update our info page so our info page
will show the error if for whatever
reason we're unable to connect so we
return but if we do if we are able to
connect because this check basically
connects us if we are able to connect
what do we want to do well if we are
able to connect then what we're gonna
say is chat app dot create chat page
doesn't quite yet exist but we'll make
it
and then chat app dot screen manager dot
current current there we go
equals chat with a capital C okay so now
we want to do is we need to create this
method the create chat page method
inside of our epic app so we'll just add
that down here now the reason why we
don't want to make this like you could
build it here but the problem is when we
make our our chat app or chat page let's
see if I can speak this morning our chat
page is listening for these connections
and but until we actually try to connect
alright we can't listen for these
connections so first we have to do this
then we can do this bit so we only
really want to
this page for real once we've connected
already okay hopefully that makes sense
if it doesn't feel free to ask but
basically as soon as we initially as we
create this page it's gonna be listening
for connections which we can't do until
we've connected right okay cool
makes sense to me anyways hopefully you
guys understand so now what we want to
say is define create chat page self and
what we want to do is basically the same
stuff that we've done before so chat
page will equal chat whoops chat page
which we also need to create but we'll
get there
then we're gonna say screen equals
screen
whoops name name equals chat then what
we want to say is screen dot add
underscore widget self dot chat page and
then self dot screen manager dot add
widgets screen so even though it's kind
of an int so Nathan it's the exact same
pattern that we've had up here and again
we're just separating it out because we
have to connect before we can run this
but as soon as we connect then we
basically run this method that adds it
to our list of possible screens so it's
still at this point we could switch to
the info or even the connect screen if
we want it or any other screen that we
want to add later we just need to run
this we can't run this at the same time
as we run these unless we connected
immediately but then it wouldn't make
sense to fill fill in the format when we
would you ever do that anyway I think
you guys get it so uh info page so we'll
just add chat page really anywhere so
we'll call this class chat page and chat
page will also be a grid layout and for
now let's just
womp copy-pasta
Dell calls cool all I want to do is just
add a quick label so I'm just gonna say
self dot add widgets label and we'll
throw in some text hey at least it
worked up to this point that's always
the call okay
so the last thing we need to do is
probably this error yeah so we need to
do the show error that can just be
really anywhere so I'm just gonna throw
that down here and that's gonna be
define show error or whatever that error
message is and then chat app dot info
info page dot update underscore info
message chat app dot screen underscore
manager dot current just in case it's
not on the info page at the moment it
should actually be on the info page
actually I probably want those spaces it
should be on the info page but later
something else might happen like maybe
while you're chatting you lose internet
or something we we lost the socket we
throw an error we want to go back to
that info page so we're just kind of
making sure we're on that info page and
then really you could hold there but
then there's really nothing for anybody
else to do so what we're gonna say here
is clocked on schedule underscore once
will do a cyst on exit and we'll do that
in 10 seconds do we have cysts imported
no so also for obsess great okay that's
a lot of code that we've just thrown
together so let's see if it works quick
guess of how many typos I've made PI - 3
TV app 1 2 3 4 come on we can do it okay
so
well well first of all let's go ahead
and just try right so we get our error
because we're not actually running the
server okay so at least we know the
error message works I just hit my Escape
key by the way now what we want to do is
let's go ahead and run the socket server
so PI - 3 7 socket server PI listening
for connections on localhost 1 2 3 4 and
let's try to run our key V app una mas
una vez mas anyway we'll hit join hey at
least it worked up to this point so we
made our connection all that we're on
our chat page and now we just need to
simply build out the chat page so that's
what we're going to be doing in the next
tutorial questions comments concerns
whatever feel free to leave them below
if you want to support the channel
support what I do you like tutorials
like this check out that beautiful
beautiful blue join button get access to
tutorials in advance get awesome
shoutouts good to hear your awesome from
me all that fun stuff so anyways yeah
questions comments concerns suggestions
whatever
feel free to lean below otherwise I'll
see you guys in the next video