Author
Released
7/7/2017Note: 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
Skill Level Intermediate
Duration
Views
- [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.
Related Courses
-
Python: Design Patterns
with Jungwoo Ryoo1h 59m Intermediate
-
1. Creating the GUI Form and Adding Widgets
-
Introduction to Python GUI4m 45s
-
Exploring widgets5m 55s
-
Adding extra features9m 2s
-
-
2. Layout Management
-
Creating menu bars6m 30s
-
Creating tabbed widgets7m 5s
-
3. Look and Feel Customization
-
Creating message boxes4m 46s
-
Using a spinbox control5m 24s
-
-
4. Data and Classes
-
Using the StringVar() type8m 14s
-
-
5. Matplotlib Charts
-
Creating our first chart2m 14s
-
Placing labels on charts5m 9s
-
Giving the chart a legend3m 32s
-
Scaling charts2m 56s
-
6. Threads and Networking
-
Creating multiple threads4m 56s
-
Starting a thread4m 56s
-
Stopping a thread4m 18s
-
Using queues6m 10s
-
-
7. Storing Data in Our MySQL Database via Our GUI
-
Using the SQL INSERT command3m 28s
-
Using the SQL UPDATE command3m 17s
-
Using the SQL DELETE command5m 52s
-
8. Internationalization and Testing
-
Localizing the GUI6m 56s
-
Setting debug watches6m 46s
-
9. Extending Our GUI with the wxPython Library
-
Creating our GUI3m 59s
-
Adding controls7m 17s
-
10. Creating Amazing 3D GUIs with PyOpenGL and Pyglet
-
Pyopengl transforms our GUI6m 27s
-
Our GUI in 3D4m 50s
-
-
11. Best Practices
-
Avoiding spaghetti code3m 52s
-
When not to use OOP2m 52s
-
Avoiding complexity5m 57s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Introduction to Python GUI