Copy-pasting code from the previous videos and making local changes to it is cumbersome. We'll restructure your code to make it less redundant.
- [Instructor] In the previous video, we added a lot of extra features to our GUI to make more advanced. In this video, we start refactoring our code to make it less redundant. So far, we've created several widgets of the same type. For example, radio button. By basically copying and pasting the same code, and then modifying the variations. For example the column number, we're refactoring some parts of the previous video's code.
So you need that code to apply this video too. I've created a new file again called AddingWidgetsInALoop.py. Here, we've turned our radio button global variables into a list. Next, we're setting a default value to the tk.InVar() variable we named radVar. This is important because while in the previous video, we had set the value for radio button widgets starting at one. In our new loop, it is much more convenient to use python's zero-based indexing.
If we did not set the default value to a value outside the range of our radio button widgets, one of the radio buttons would be selected when the GUI appears. While this in itself might not be so bad, it would not trigger the callback and we would end up with a radio button selected that does not do its job, that is change the color of the main WinForm. On this line, we're replacing the three previously hard coded creations of the radio button widgets with a loop, which does the same.
It is just more concise with fewer lines of code and much more maintainable. For example, if we want to create a hundred instead of just three radio button widgets, all we have to change is the number inside python's range operator. We would not have to type, or copy and paste, 97 sections of duplicate code. Just one number. This line shows the modified callback, which physically lives above these lines. Let's see the output.
As you can see, running this code will create the same window as before, but our code is much cleaner and easier to maintain. This will help us when we expand our GUI in the upcoming videos. Nice. This video concludes the first section of this course. In our first GUI we added a label, buttons and text boxes to the form. After that, we explored widgets in more detail.
And also, created check boxes, radio buttons, and scroll text widgets. Finally, we refactored our code to make it less redundant and more scalable. All the following videos in all the next sections will build upon the GUI we have constructed so far, greatly enhancing it. So, the next section will discuss layout management of our GUI.
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
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.