How to do it...

We will create a new Python module, and the following screenshot shows both the code and the resulting output:

GUI_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 create an instance of a class.

Usually, we use this instance assigned to the win variable to start the main event loop later in the code, but here, we are not displaying a GUI, rather, we are demonstrating how to use the tkinter StringVar type.

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 type and assign it to our Python strData variable.

After that, we use our variable to call the set() method on StringVar and after setting to a value, we get the value, save it in a new variable named varData, and then print out its value.

In the Eclipse PyDev console, towards the bottom of the screenshot, we can see the output printed to the console, which is Hello StringVar.

Next, we will print the default values of tkinter IntVar, DoubleVar, and BooleanVar types:

GUI_PyVar_defaults.py

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.149