Learn how to leverage the Python programming language to build graphical user interfaces (GUIs).
- [Instructor] Hi, and welcome to section one, GUI in Python. In this section, we are developing our first GUI in Python. We will create a working GUI application that consists of labels, buttons, text boxes, combo boxes, check buttons in various states as well as radio buttons that change the background color of the GUI.
In this video, we're going to create our first GUI. Then, we'll prevent it from being resized. Python is a very powerful programming language. It ships with the built-in Tkinter module. In only a few lines of code, four to be precise, we can build our first Python GUI. To follow this video, a working Python development environment is a prerequisite. The IDLE GUI that ships with Python is enough to start.
IDLE was built using Tkinter. In this video, we'll be using Atom. Cool, let's jump right in. Here are the four lines of Python code required to create the resulting GUI. On the first line, we import the built-in Tkinter module and alias it as tk to simplify our Python code. In line two, we create an instance of the tk class by calling its constructor, the parentheses appended to Tk, turn the class into an instance.
We are using the alias tk so we don't have to use the longer word Tkinter. We are assigning the class instance to a variable named win, short for window. As Python is a dynamically-typed language, we did not have to declare this variable before assigning to it. And we did not have to give it a specific type. Python infers the type from the assignment of the statement. Python is a strongly-typed language, so every variable always has a type.
We just don't have to specify its type beforehand like in other languages. This makes Python a very powerful and productive language to program in. In line three, we use the instance variable of the class win to give our window a title via the title property. In line four, we start the windows event loop by calling the mainloop method on the class instance win.
Up to this point in our code, we created an instance and set one property, but the GUI will not be displayed until we start the main event loop. Okay, let's execute this code. The filename is FirstGUI.py. Here's our first ever GUI. Cool, isn't it? Please note that in this file we used a minimum amount of Python code to create our first GUI program. However, throughout this course, we will use object-oriented programming when it makes sense.
Okay, let's now move on to preventing the GUI from being resized. How do we do that? Well, here's another file named NotResizable.py. This file extends the previous one. What is different then? Line four prevents the Python GUI from being resized. Running this code will result in a GUI similar to the one we created just now. Let's see the output. As you can see, the user can no longer resize it.
Also, notice how the maximum button in the toolbar of the window is grayed out. Why is this important? Because once we add widgets to our form, resizing can make our GUI look not as good as we want it to be. We will add widgets to our GUI in the next videos. Resizable is a method of the tk class, and by passing in (0,0), we prevent the GUI from being resized.
If we pass other values, we hardcode the X and Y startup size of the GUI, but that won't make it non-resizable. Nice, we created our first ever GUI in this video. In the next one, we'll add a label to our form.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating buttons and widgets
- Adding labels and features
- Expanding a GUI dynamically
- Aligning frames and embedding frames
- Creating menu bars, message boxes, and tooltips
- Using module-level global variables
- Coding in classes
- Using Matplotlib to create charts
- Working with multiple threads, queues, and TCP/IP
- Using URLOpen to read data from websites
- Localizing a GUI and preparing for internationalization
- Testing a GUI using unit tests and Eclipse PyDev IDE
- Using the wxPython library
- Using Tkinter, PyOpenGL, and Pyglet