The aim of this video is to add more functionality to your GUI.
- [Burkhard] In the previous video, we explored the world of widgets in detail. We'll go over adding some extra features to Window form in this video. We will add three Checkbutton widgets, each with a different initial state. Then, we will create three Tkinter Radiobutton widgets. We will also add some code that changes the color of the main form, depending upon which Radiobutton is selected. Finally, we'll see how to add scrolled text widgets.
As always, the code used in this video extends the previous videos. In a new file called CreateCheckButton.py, we're creating three Checkbutton widgets that defer in their states. The first is disabled, and has a check mark in it. The user cannot remove this check mark, as the widget is disabled. The second Checkbutton is enabled, and by default, has no check mark in it. But the user can click it to add a check mark.
The third Checkbutton is both enabled and checked by default. The users can uncheck and recheck the widget as often as they'd like. Let's see how we wired these up. In these lines, we create three variables of type IntVar. Then, for each of the variables, we create a Checkbutton, passing in these variables. They will hold the state of the Checkbutton if they're unchecked or checked. By default, that is either zero, unchecked, or one, checked.
So, the type of the variable is a Tkinter integer. We placed these Checkbutton widgets in our main window so the first argument passed into the constructor is the parent of the widget. In our case, it's win. We give each Checkbutton a different label via its text property. Setting the sticky property of the grid to tk, w means that the widget will be aligned to the west of the grid.
This is very similar to Java Syntax. And it means that it will be aligned to the left. When we resize our GUI, the widget will remain on the left side, and not be moved towards the center of the GUI. After that, these two lines place the check mark into the Checkbutton widget by calling the select method on these two Checkbutton class instances. We continue to arrange our widgets using the grid layout manager, which will be explained in more detail in section two.
Let's check the result now. Running the new code results in a GUI that looks something similar to this. Great! Now, let's move on to using Radiobutton widgets. This video extends the previous videos. We're adding the following code to the previous video into a file called RadioButtonWidget.py. First, we create some module-level global variables which we will use in the creation of each Radiobutton.
We'll also use them in the callback function that creates the action of changing the background color of the main form using the instance variable win. We are using global variables to make it easier to change the code. By assigning the name of the color to a variable and using this variable in several places, we can easily experiment with different colors. Instead of doing a global search and replace of a hard coding string, which is prone to errors, we just need to change one line of code, and everything else will work.
This is known as the DRY Principle, which stands for Don't Repeat Yourself. This is an OOP concept that we will use in later videos of the course. The names of the colors we are assigning the variables, color one, color two, and such, are Tkinter keywords. Technically, they are symbolic names. If we use names that are not Tkinter color keywords, then the code will not work.
Here's the callback function that changes the background of our main form, win. Depending upon the user selection, on line 62, we are creating a tk.IntVar variable. What is important about this is that we're creating only one variable to be used by all the three Radiobuttons. Why? Because no matter which Radiobutton we select, all the others will automatically be unselected for us. These lines here create three Radiobuttons, assign them to the main form, and pass in the variable to be used in the callback function that creates the action of changing the background of our main window.
Running this code and selecting the Radiobutton named Gold will create a window like so. Splendid! While this is the first video that changed the color of a widget, quite honestly, it looks a bit ugly. Don't worry, though. A large portion of the following videos in this course explain how to make our GUI look truly amazing. If you want to know the available symbolic color names, you can look up at the official TCL manual page.
Some of the names create the same color, so Alice Blue creates the same color as Alice Blue. In this video, we used the symbolic names Blue, Gold and Red. Okay, let's see how to add scrolled text widget now. Scrolled text widgets are much larger than simple entry widgets and span multiple times. They are widgets like Notepad and Wrap Lines, automatically enable vertical scroll bars.
When the text gets larger than the height of the scrolled text widget. As always, this video extends the code from previous videos in a file called ScrolledTextWidget.py. How do we create a scrolled text widget? Here's the code for it. Here, we are importing the module that contains the scrolled text widget class. Add that to the top of the module just below the other two important statements, import statements.
Down here, we define the width and heighth of the scrolled text widget we are about to create. These are hard coded values we are passing into the scrolled text widget constructor. These values are magic numbers found by experimentation to work well. You might experiment by changing srcolw from 30 to 50, and observe the effect.
Here, we are setting a property on the widget by passing in wrap=tk.WORD. By setting the wrap property to tk.WORD, we are telling the scrolled text widget to break lines by words, so that we do not wrap around within a word. The default option is tk.CHAR, which wraps any character, regardless of whether we are in the middle of a word. Let's run this code.
As you can see, we can actually type into our widget. And if we type enough words, the lines will automatically wrap around. Awesome! Now, once we type in more words, then the height of the widget can display, the vertical scroll bar becomes enabled. This all works out of the box without us needing to write any more code to achieve this. How did we achieve this? Setting the column span property of the grid widget to three for the scrolled text widget makes this widget span all three columns.
If we did not set this property, our scrolled text widget would only reside in column one, which is not what we want. Great! In this video, we saw lots of additional features that we could add to our window form, like check boxes, Radiobuttons, and scrolled text widgets. In the next video, we'll add several widgets in a loop.
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