You can add more features to your GUI, namely buttons and text boxes.
- [Narrator] In the previous video, we looked at adding a label to our GUI. In this video, we are going to take a look at adding two features to the GUI, buttons and text boxes. This video extends the previous one. Here we are adding a button, that when clicked, performs an action. In this video, we will update the label, the label we added in the previous video, as well as updating the text property of the button. Here we are now assigning the label to a variable, and in this line we use this variable to position the label within the form.
We will need this variable to change its properties in the clickMe function. By default, this is a module level variable so we can access it inside the function, as long as we declare the variable above the function that calls it. ClickMe is the event handler that is being invoked once the button gets clicked. Here we create the button and bind the command to the clickMe function. We also change the text of the label to include red which might otherwise not be obvious.
When you run the code you can see that the color did indeed change. Lines nine and 19 both use the grid layout manager which will be discussed in the following section. This aligns both the label and the button. Let's see our output. The file that we just created is named as CreateButtonChangeProperty.py. Double click on it. Now, this is how our label looks before clicking the button.
After clicking the button the color of the label has been changed and so has the text of the button, awesome. The next task up our sleeve, creating text box widgets. In Tkinter, text box widget is called Entry. In this video, we will add such an Entry to our GUI. We will make our label more useful by describing what the Entry is doing for the user. This file is named as TextBoxWidget.py. It builds upon the previous file.
On this line, we are getting the value of the Entry widget. We're not using OOP yet, so how come we can access the value of a variable that was not even declared yet? Without using OOP classes, in Python procedural coding, we have to physically place a name above a statement that tries to use that name. So how come this works? The answer is that the button click event is a call back function and by the time the button is clicked by a user, the variables referenced in this function are known and do exist.
Life is good! This line gives our label a more meaningful name because now it describes the text box below it. We moved the button down, next to the label, to visually associate the two. We are still using the grid layout manager which we'll talk about in more detail in section two. Moving on, here we're creating a variable name. This variable is bound to the Entry and in our clickMe function we are able to retrieve the value of the Entry box by calling get on this variable.
This works like a charm. Note that Python is a dynamically typed language and infers the type from the assignment. For example, if we assign a string to the variable name the variable will be of the type string. Using Tkinter we have to declare the variable name as the type tk.StringVar before we can use it successfully. The reason being, Tkinter is not Python. We can use it from Python but it is not the same language.
Let's run this code. Our GUI looks like this. Let me type Monty Python and click this button. We see that while the button displays the entire text we entered and more, the text box entry widget did not expand. The reason for this is that we had hard coded it to a width of 12 in here. Excellent, we got introduced to buttons and text box widgets in this video.
In the next one, we'll explore widgets in more detail.
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