Previously, we build these Python codes
and this Kivy code. So it's a Kivy app,
which you execute by executing the
Python code, not the Kivy code. So
here, press the Run button and this is
what we have. Let's admit it. It's a
good looking interface. But it's rather
cold for now, because when we press
these buttons, nothing happens. So let's
go ahead and implement the Sign Up
button. So what we want to do is, when
the user presses the Sign Up button, we
want to change the page the screen to
another screen with another set of
widgets. For that, we need to assign a
function call to the Sign Up button. So
let's go ahead and go to the Kivy code,
locate the button Sign Up, which is in
here. And so the button gets these
attributes the text attributes, but it
can also get another attributes
[No audio]
called on press, on_press. What that
attribute does is when the user presses
a button, you are going to call the
root.sign_up function. Don't forget the
call parenthesis. So what is the root and
what is signup? Root is a special name
you can use inside a Kivy file. And root
refers to the class of the RootWidget.
The RootWidget in here where we are a
is the LoginScreen. So the class of
LoginScreen which is a LoginScreen. So
actually roots refers to the LoginScreen
class. And now we're using this
dot notation. And we're using the method
of LoginScreen. So sign_up of LoginScreen
where there is no sign_up methods.
but, we can create one, define sign_up,
use the self parameter for class
methods. And then let's do something
simple for now. Let's just print out.
[No audio]
Sign up button pressed is just a string,
which means it's going to be printed out
on the terminal. Let's try this, save
the Kivy file and run the Python file and
then press Sign Up. And as you can see
in the terminal Sign Up button press
string is bringing out anytime I use the
Sign Up button. So it is as simple as
that you use attribute on press at
anytime the button is pressed sign_up
will be called this function. So now
what we want to do for real is to switch
the screen. But first we need to create
a screen. The way you create screens is
in the Kivy file. So we have the LoginScreen
here. Now we want to create, now
let's call this SignUpScreen. After
the root, you use the column, a tab to
indent. And here is how this new screen
will look like. So we have a label, we
have two text input boxes, and we have a button.
[No audio]
And everything is inside a GridLayout
[Author typing]
with one column, this one column will simply have a Label
[No audio]
with text Sign up for a space
journey. Why not? Feel free to use your imagination.
But make sure this is inside quotes. So it's a string.
[Author typing]
Then comes a TextInput.
[No audio]
Let's use as hint_text.
[No audio]
The Username string. So what is going to
be shown in the textbox as a hint.
[No audio]
Another TextInput with hint_text, you guessed it,
Password, and lastly a Button
[No audio]
with text Submit or you can write Sign Up whatever
you like. So these are just strings you
can use whatever you like. So the idea
is that when the user presses the Submit
button, this data, so the username and
the password will be sent to the
database where we are saving the
usernames and passwords. So this
database will be readable. So we can
write it, we can add more usernames to
this database. And it will also be
readable, which means when a user tries
to login, we are going to read this
database to see if the username and
password that the users trying to send
with a Login button, are or not in this
database. And this database is going to
be a JSON file. Which is a simple way to
handle Username and Password data.
[No audio]
Right.
[No audio]
Now, if you save this Kivy file
and run the Python script, nothing is
going to happen because the Sign Up
button is still printing out this simple
string. And the screen we just created
is not being sent everywhere to be
displayed. So we are not giving any
orders to display the SignUpScreen. So
the next thing to do is to list the
SignUpScreen among the RootWidget widgets.
So to say the name sign_up_screen.
So screens have to have a name. The reason
for that is because in here now, in the
Sign Up button, what we can do is the
magic of doing self.manager.current
equal to sign_up_screen.
So sign_up_screen is this string here that we
have just created. Save a Kivy file and
run the Python file to see what's going on.
[No audio]
So you may get this error from time
to time kivy.factory.FactoryException,
Unknown class SignUpScreen, which means
it's an error that occurred in your Kivy
file. And the class SignUpScreen which is
this one here it's unknown because there
is no class in the Kivy file in the Kivy
library as such as it is for GridLayout,
Labels, these are all classes in the
Kivy library. So SignUpScreen is not a
class in the Kivy library and it's not a
class declared in the Python file. So
what you need to do is, just create a
SignUpScreen class inheriting from
Screen. And for now just to pass. Execute again
and Sign Up and this our new screen.
So we got the Label, two TextInput boxes
and one Submit Button which doesn't do
anything for now, because this SignUpScreen
is connected to the class we just
created the SignUpScreen class. So,
whatever actions will be taken in this
page in this screen from the user are
going to correspond with some functions
that we are going to add to the SignUpScreen.
And let me explain this line
here self.manager.current. So
self is referring to the instance of
this current class or the class where
self is. So LoginScreen object
LoginScreen is the class and self is the
object that has been instantiated from
this class that has been created from
this class. And then manager, manager is
a property of screen. So since this
class is inheriting from screen to
Screen is a parent and LoginScreen is a
child and the parent is giving the child
all its methods, all its properties, all
its attributes, including manager of
course, so self is able to access
manager and then manager has its own
attribute which is current. So current
of manager and manager of LoginScreen.
And this current attributes will
get the name of the screen you want to
switch to which is SignUpScreen, this
SignUpScreen in here, which is a widget
we have created in here.
[No audio]
And that's the idea. This is how you switch
between screens using Kivy. Thanks.