A Tkinter GUI is single-threaded. Every function that involves sleep or wait time has to be called in a separate thread; otherwise, the Tkinter GUI freezes. Learn how to create multiple threads.
- [Instructor] Previously, we saw Matplotlib charts. This, is section six: threads and networking. In this section, we will start with an introduction to multithreading; then, we'll learn how to start a thread, and then stop it. We'll also learn how to use queues, and then, passing queues among different modules. After that, we'll see how to copy files to our network, then, we'll see how to communicate via networks.
Finally, we'll read data from websites. In this video, we will create multiple threads using Python. This is necessary, in order to keep our GUI responsive. We'll prepare our GUI to use threads by first increasing the GUI size. We'll then import the thread class from the python threading module. After that, we'll create a method that we call in a thread from within our GUI.
When a process in created, the process automatically creates a main thread to run our application. This is called a single-threaded application. For our Python GUI, a single thread-threaded application will lead to our GUI, becoming frozen as soon as we call a longer-running task. In order to keep our GUI responsive, and not make it freeze, we have to use multi-threading. Processes are isolated by design from each other, and do not share common data.
In order to communicate between separate processes, we would have to use inter-process communication, IPC. Multiple threads run within the same computer process memory space. There's no need for inter-process communication, aka IPC, which would complicate our code. In this video, we will avoid IPC by using threads. For this video we will use the code files, which we have used in section four, such as OOP.py, and OOP_ToolTip.py.
Let's first check the output of this program. Okay, not let's modify it a little. First, we will increase the size of our scroll-text widget, making it larger. Let's increase scrolW to 40, and scrolH to 10. When we now run the resulting GUI, the spin box widget is center aligned in relation to the entry widget above it, which does not look good. We'll change this by left-aligning the widget.
Add sticky=W to the grid control to left-align the Spinbox widget. Let's check the output again. The GUI could still look better, so next, we will increase the size of the entry widget, to get a more balanced GUI layout. Increase the width to 24 as shown. Let us also slightly increase the width of the Combobox to 14.
Let's first check the output of this program. Running the modified and improved code results in a larger GUI, which we will use for this, and the upcoming videos. In order to create and use threads in Python, we have to import the thread class from the threading module. Let's add a method, to be created in a thread, to our OOP class. We can now call our threaded method in the code.
Saving the instance in a variable. Now, we have a method that is threaded, but when we run the code, the line: hi, how are you, doesn't get printed to the console. We have to start the thread first, before it can run, and the next video will show us how to do this. However, let's see what happens if, set a break point, after the GUI main event loop.
This proves that we did indeed create a thread object, as could be seen in the eclipse IDE debugger. Great! That's about it for this video. We successfully created multiple threads here. In the next video, we'll start a thread.
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
Skill Level Intermediate
Python GUI Development with Tkinterwith Barron Stone4h 55m Intermediate
1. Creating the GUI Form and Adding Widgets
2. Layout Management
3. Look and Feel Customization
4. Data and Classes
Using the StringVar() type8m 14s
5. Matplotlib Charts
6. Threads and Networking
7. Storing Data in Our MySQL Database via Our GUI
8. Internationalization and Testing
9. Extending Our GUI with the wxPython Library
10. Creating Amazing 3D GUIs with PyOpenGL and Pyglet
11. Best Practices
- 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.Cancel
Take 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.