We'll learn how to save data from the Tkinter GUI into variables so that you can use that data. After that, we'll see how to capture data from a widget.
- [Instructor] In the last section, we learned how to customize the look and feel of our GUI. In this section, we will save our GUI data into Tkinter variables. We will use data and OOP classes using Python 3. We'll see how to use StringVar, then how to get data from a widget, then we'll see how to use module-level global variables. We'll then refactor all our procedural code in classes to improve the GUI.
Finally, we'll see how to create reusable GUI components. There are built-in programming types in Tkinter that differ slightly from the Python types we're used to programming with. StringVar is one of those Tkinter types. This video will show you how to use the StringVar type. Later on, we'll show you how to capture data that the user enters in a variable. We are learning how to save data from the Tkinter GUI into variables so we can use that data.
We can set and get their values, very similar to the Java getter-setter methods. Here are some of the available types of coding in Tkinter. StringVar, IntVar, DBVar, which is DoubleVar, and BLVar, Boolean. Different languages call numbers with decimal points floats or doubles. Tkinter calls it DoubleVar for what in Python is called a float data type.
Depending on the level of precision, floats and double data can be different. How to do it? Well, from this section onwards, I'll be using Eclipse as our IDE. PyDev is an interpreter in Eclipse which is used to run Python programs. We're creating a new Python module in a file called StringVar.py. First, we import the Tkinter module and alias it to the name tk.
Next, we use this alias to create an instance of the Tk class by appending parentheses to Tk, which calls the constructor of the class. This is the same mechanism as calling a function, only here we're creating an instance of a class. Usually we use this instance assigned to the variable win to start the main event loop later in the code but here, we're not displaying a GUI but demonstrating how the Tkinter StringVar type.
Even then, we still have to create an instance of Tk. If we comment out this line, we will get an error from Tkinter so this call is necessary. Then, we create an instance of the Tkinter StringVar StringVar type and assign it to our Python strData variable. After that, we use our variable to call the set method on strData and after having it set to a value, we then get the value and save it in a new variable named varData and then print out its value.
Okay, let's run this file. In the Eclipse PyDev console, towards the bottom of the screen, we can see the output printed to the console, which is "Hello StringVar." Next, we will print the default values of Tkinter's IntVar, DoubleVar, and BooleanVar types. Here's the code for that. Let's check the output. As you can see, the default values do not get printed as we would have expected.
The online literature mentions default values, but we won't see those values until we call the get method on them. Otherwise, we just get a variable name that automatically increments, for example, PY_VAR3. Let's see if anything changes if we assign the Tkinter type to a Python variable. Let's now see the output. As you can see, the outcome does not change and we still do not get the default value.
Here, we're focusing on the simplest code which creates PY_VAR0. The value is PY_VAR0, not the expected zero. Let's add a call to the get method. Now we can see the default value. We did not call set, so we see the default value automatically assign to each Tkinter type once we call the get method on each type.
Notice how the default type of zero gets printed to the console for the IntVar instance we saved in the intData variable. Okay, let's see what we have next in store for us, getting data from a widget. When the user enters data, we want to do something with it in our code. This video shows how to capture data in a variable. In the previous file, we created several Tkinter class variables, but they were standalone.
Now we are connecting them to our GUI using the data we get from the GUI and storing it in Python variables. We're continuing to use the Python GUI that we were building in the previous section. How to do it. Here's a new file, Spinbox.py. We are assigning a value from our GUI to a Python variable. We've added these two lines of code towards the bottom of our module just above the main event loop.
Running the code will give us this result. Here, we are retrieving the current value of the Spinbox control. So what did we do here? We placed our code above the GUI main event loop and so the printing happens before the GUI becomes visible. We would have to place the code in a callback function if we wanted to print out the current value after displaying the GUI and changing the value of the Spinbox control.
We created our Spinbox widget using this code, hard-coding the available values into it. We can also move the hard-coding of the data out of the creation of the Spinbox class instance and set it later. Let's see how to do that.
It does not matter how we create our widget and insert data into it because we can access this data by using the get method on the instance of the widget. Let's check out the output. In order to get values out of our GUI written using Tkinter, we use the Tkinter get method on an instance of the widget we wish to get the value from. In this example, we use the Spinbox control but the principle is the same for all widgets that have a get method.
Once we have gotten the data, we are in a pure Python world. Tkinter did serve us to build our GUI. Now that we know how to get the data out of our GUI, we can use this data. Awesome! That's about it for this video. We learned how to use StringVar and capture data from a widget. In the next video, we'll see how to use global variables.
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