Hello everybody! How are you guys?
We’ve already learned a lot of things about Python But all of our programs until now, were using the terminal and the command prompt. Well, now we have sufficient knowledge for creating our own graphical interfaces. And this is what we are going to do in this video!
So, let’s go! Very well. The first thing that we are going to do is to install one package to help us creating our graphical interfaces. There are many out there, but we are going to use this one, which is called Kivy. It’s a very interesting framework, because it works in many different platforms it is GPU accelerated, and so you can even build games with it, and it is very easy to work with. So, to install it, we will click the download link, and it will show many different instructions to install it into different platforms. Ok, so if you use Windows, there are some instructions here You can install it using pip, ok? You need to run these codes here that are into these gray boxes. So, here you are going to update pip, wheel and the setuptools, right? So that you can install new packages. And here are the dependencies, ok? This docutils, pygments, pypiwin32, kivy.deps.sdl and glew. And, if you need to use something related to video and streaming, you have to install this package here too, ok? Ok, then you just have run pip install kivy. They use this python -m but if you only write pip install kivy, like we were doing in the last videos, it will also work, ok? Very well. And if you are using Ubuntu the instructions are here. You will need to add this repository. I sugest adding the stable one. Then you update your repositories and install it: sudo apt-get install python-kivy or python3-kivy, if you want the python3 version, which is what we are using. So, I sugest using this command here. As I use Arch Linux here things are easier. I just have to write here python-kivy, put the password and it’s done. Very well, now that it is installed, we are going to code our graphical interface. Well, we are going to create a file named main.py. And now, we have to understand how this Kivy works. So, to create our graphical interface, we are going to create an App. In the Kivy jargon, we have to import this App, which is the responsible to make everything work, right? It will open a window, and make the things to make our program work. So we are going to import it in the following manner: from kivy.app import App. Ok? Uppercase App. Very well. With this, we can only create a window ok? Just the very basic. Then we have to build upon it, with what they call widgets. They are like little apps, ok? Little pieces that we will assemble to create our interface. For example, a button, a label, which is a text. In short, there are lots of things. In this video, we are going to create a button. It won’t do anything yet but we will run the app, a window will appear with a button, which we can click. So, to create this button, we are going to import it, right? So: from kivy.uix.button import Button, ok? We imported the button and, now, we will create a class that inherits this class here, App, ok? So this is what our App is going to be, this class. Ok, so we will put here the name of our application, which will be Test Then, we will inherit from this App class. So, it’s going to have all of the functionalities of this App class. And we are going to define a method which is called build, which is going to initialize the interface, ok? And construct our application. In other words, it’s here that we are going to put our button. So it will return it. Everything this method returns is going to be constructed in the window of our application. We are going to put here the button. We will instantiate the button. Very well. So, this is the code of our application. It’s this class with the build method, that constructs the app. And, then, what it returns is what is going to appear in our window. That simple. So, up to now, we created this class, and here we are going to instantiate it, like follows. We will create it, right? Test() And, then, as we inherited from this class App, it is going to have a run method So, this method here that will run our app, test. Ok? So, this is the code. This is all we have to do to create a graphical interface. So, let’s run this and see what happens. Very well. You guys saw that it initialized here a lot of things. So, this is a very complicated thing, but it let’s you see all it’s doing. You don’t need to worry with anything of this, ok? It already initialized many things with OpenGL, in short, a lot of things. It opened a window and, here, there is a button in which we can click. You saw that it turns blue, right? So this is our graphical interface. Ok, so this button is kinda boring, right? It doesn’t have anything. Let’s put some text in it. We are going to edit the main file and this button here, this class of buttons, we can initialize with a lot of arguments. One of them is this text which is going to be the displayed text of the button. So here, we are going to write: Hello World, ok? Very well. It’s that simple. We will run this and our button will have a text: Hello World, right? Ok, so it’s all for this video. I hope that you enjoyed and until next time 🙂