Let’s start with this article with the launch of a terminal in the Ubuntu 20.04 Linux system. After launching it, we have been using the “update” query on the shell to make our system up to date. Provide your password if asked while an update continues.
After the successful system update, you need to install the Tkinter module on your system via the apt install instruction. You need to use the title of a package as “python3-tk” in a query as we have done it in the image so far. The Tkinter GUI module of python would be installed in a few seconds. You can see that we have already configured it on our end.
Example 01:
Let’s start this article by creating a very first example of python that would be using the Tkinter module in it to create a GUI and will be using the StringVar type variable values. Thus, we have started the program code with the import of all the sub-classes and functions from the Tkinter module of python via the “import” keyword followed by the “*” character as shown in the first line.
After that, we have been importing the ttk object from the GUI ttk class using the Tkinter module. In the third line of code, we have been using the variable “t” to get the instance of a Tkinter using the TK() class constructor. The “t” variable would be then utilized to add a title for the GUI to be created on the shell using the title() function. The same “t” variable object has been used to call the geometry function of Tkinter to set the total area or size for a GUI to be created. In the 6th line of code, we have initialized a variable “v” by calling the StringVar() function and added a value “Kalsoom” into it.
In the next line, the label has been created using the Label() built-in function of Tkinter using the “t” variable object and string variable “v” as label text. Also, we have specified the font for the label along with its background color to red, foreground color to yellow (color of text), and padding of 30. In the last line, we have been using the mainloop() executor function to iterate the overall Tkinter loop of widgets. Let’s save our code first before the execution.
The python3 keyword has been used in the query area along with the “new.py” file name for the python code execution.
Tkinter GUI of python has been launched with the title “Variables” containing a label “Kalsoom” created with the help of StringVar variable type.
Example 02:
Let’s take a look at another example of python Tkinter to use a StringVar type variable in it. So, we have started this illustration code with the import of the Tkinter module as “tk” and its ttk class object. The python script contains the main App class instantiated from the “Tk” class of a Tkinter via its “tk” object.
Within this main App class, we have a total of three functions. 1 main() function doesn’t belong to the App class. The execution of a python script started from the main() function has been calling the App() class constructor using its instance app. The constructor “__init__” runs and gets the instance from a super class constructor. That instance “self” would be used to specify the title and size for a GUI to be created using the title() and geometry() methods of the Tkinter module. Another variable “Name” has been initialized with the StringVar() function via the “tk” object to get its value.
After that, the Show() method has been called by this constructor. The Show() function has been started with the setting of padding for a GUI from all the sides and a Label() will be formed using the “ttk” class module of a Tkinter. The text it would be containing is “Enter String:”. We have been creating a textbox using the Entry() function of Tkinter via the “ttk” object. Its text variable parameter identifies that it would be getting a value from a user as input in the “Name” variable. The grid() function has been applied to the textbox to set its specific padding as initialized at the start of the Show() function. The textbox has been set to focus and a button “b” with the title “Submit” would be used to call the “submit” function that has been configuring the user input “Name”’ to a result variable via the “config()” function.
The grid() function sets the padding for the button and a new Label named “result” has been generated taking the value from a text box.
Run the python file first.
The shown below GUI of Tkinter is generated. Input some string values in the textbox and tap on the “Submit” button.
The inputted string value would be displayed as a label on the GUI dialog screen as shown.
Conclusion
This article is the bundle of examples to use the StringVar variable type to use the string values in different widgets. Using the illustrated examples, you will be able to understand the python Tkinter module to create different text-based widgets which can use StringVar variable type.