This video introduces you to Python's built-in GUI framework, Tkinter. Start with the fewest lines of code required to build a working GUI, and then change the GUI's default size and also fix its size.
- [Instructor] Hello, and welcome to Python projects. In this video section, we will create a GUI using Python. We will be using their built in Tkinter module. Tkinter ships with Python so we do not have to install any third party modules. What is Tykinter, here's a definition, Tkinter is basically a thin layer on top of Tcl/Tk.
And that's where the name comes from. So inter means the interface of Python interface toward Tcl/Tk, so it's Tkinter. Tkinter is a GUI framework so what that gives us is that in only a few lines of code, we can create a fully functional graphical user interface in Python. So the way to do this is we import the Tkinter module and here we alias it as Tk, just to make the name a little shorter.
And one thing to notice is that in Python 3, the t in Tkinter is in lower case, in Python 2.X, it's actually a capital T. So when you look online you might find lots of examples that have a capital T. and if you try those examples, just be aware to change it for Python 3 was to make the t lowercase. So after, importing Tkinter we create an instance of the class and assign it a local variable which we call win.
And now we can use the reference we have to the instance and edit title, so we call the title method. It accepts a string and we give it Python GUI as a string which will become the title. Now once, we run this code here, nothing will happen until we call the mainloop on the win variable. The main loop is an endless loop, it's the main Windows loop that keeps the GUI up and running.
And we get out of the endless loop by clicking the red x. So let's run it, so as we can see here, four lines of Python code, we have a fully functional graphical user interface. You can minimize it and get it back, you can resize it it in both directions, we can fully maximize it, we can make it smaller again, and move it around. And when we click the red x it closes.
We know that our GUI comes with a default size. Now in order to learn what that default size is, we can call the update method on our win variable and then after we've updated, we can call the winfo width and winfo height message and we can print them out. So when we run our GUI, we have the same size here that prints out width and the height being 200 pixels each. And of course we have to call the master down here before the main event loop.
Now, we can also increase the size, and we want to do that. So, in increased window widths, we are going to set the widths to 300 instead of 200 pixels. And we set the high to 1, the 1 doesn't mean 1 pixel, 1 is actually meaning the default value which should be 200. So when we now run our GUI again, you can see that it's wider, it still has the same height. So here the width says 300, high is 200 and what's interesting is you can make it wider horizontally but we can't make it shorter than 300.
So we can't reduce it, so what that means is our Python GUI text in the GUI will always be visible. On the other hand, we can do this, so we can still resize it this way, we can also maximize it, reduce it again. There's another thing that we can do and that is to prevent the GUI from being re sizable. So if you set re sizable to 00, it means you cannot resize the GUI, so now when we run it, you get the same GUI, 300 x 200 pixels.
You can see that the maximize button is grayed out, the minimize one is there, you can get it back. The x is still there, but now when we try to resize it in either direction, the x or y, we cannot do it. So the size is fixed now, it's nice so the title's always visible and sometimes this is what you want. Sometimes you don't want the user to accidentally screw up the GUI by dragging some of the borders of the GUI and making the GUI look kind of funny.
So that's one little trick to know in resizeable 00 prevents the GUI from being resized and maximized. And we just close it clicking the red x.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service