Python

Tkinter ComboBox

The ttk package, a unique modification of Python Tkinter, introduces this additional component. The Python Tkinter ComboBox exhibits each choice from a drop-down menu 1 at a moment. It has a contemporary appearance, making it suitable for locations where exposure is crucial. The gadget Entry is a class ability of the Python ComboBox. As a result, it adds some additional choices and functions while also inheriting several from the Entry class.

A crucial widget that may be seen in several programs is the ComboBox. The user is given a list of alternatives to choose from. It has several values, and only ever displays one at a moment. Today’s tutorial will cover how to use the ComboBox in Linux using Tkinter.

Example 1:

Let’s get started with the very first example of the Tkinter module in Python. We create a new Python file with the “py” extension via the terminal console. After that, you can use any editor of your choice to open the newly generated Python file from the file explorer. We start this Python code with the import of the Tkinter library in the code along with all its sub-objects, classes, and built-in entities.

We importe its ttk object to be used in the code. First, we call the Tk() function of Tkinter to add a new value for the object “t”. The object “t” is used to call the geometry function to create a graphical user interface of “200×150”. Within this first example, we utilize the “frame()” method of Tkinter to create a GUI frame at the console screen. The object “f” of the frame is closed here using the Tkinter pack() function.

After this, we create a list “l” of string types containing a total of 5 string values in it. After this, we utilize the Tkinter ttk object to call the ComboBox function of Tkinter within the frame “f”. The list “l” is passed to it. This ComboBox is saved to the variable “C”. We set the label for the ComboBox using the “set” function and pack the ComboBox with exact paddings. Now, we execute the mainloop() function to execute the overall Tkinter program.

After completing the Python script, we save the code with Ctrl+S and come back to the shell console of the Linux system. We try the Python3 instruction in the shell followed by the name of a Python file to execute the file as displayed in the following:

$ python3 test.py

After the query execution, we get the following Tkinter GUI at our console screen with the title “tk”. The GUI screen contains a comboBox, i.e. a drop-down list, with the title “Choose 1 Color” and a triangle sign to open it.

After tapping on the triangle sign, the long drop-down list is shown with all its available options. You can see that we have a total of 5 options available to choose from.

Let’s say, you choose the color “Black” from the drop-down list. You will see that the selected color is shown on the title area of a drop-down ComboBox. The remaining list is hidden.

Example 2:

Let’s take a look at another example using a different method to create a ComboBox in the Tkinter this time. Thus, we start this Python code with the import of the Tkinter module in the same Python file as “tk” along with importing the ttk class of it. After that, we import the showinfo() function from the message box class of the Tkinter module. Also, we import the month_name variable from the calendar module of Python.

We call the tk() function with the tk object of the Tkinter module and save the constructor result in the variable “t”. The geometry function is called with specified parameters to create a GUI of Tkinter with a specific size. The title() function is called with a parameter “Combobox Illustration” to title the Tkinter GUI and the Label function from the ttk class to create a label “l”. The pack function is used to fill the label “l”. The variable “mn” is created with the StringVar() function of the “tk” class. The ComboBox “mcb” is created using the ComboBox function and variable text “mn”. The mcb ComboBox is filled with string values up to 13 via the month_name variable used within the “for” loop.

The state for the “mcb” ComboBox is set to readonly and it is packed. The choose() function is created to call the showinfo() function to create an alert dialogue box with the title “Confirmation” and a message “You have chosen {whatever the value}. The bind() function is called with the “mcb” ComboBox by adding the “ComboboxSelected” parameters and “change” function. The mainloop() function is executed to loop out the Tkinter program. Let’s save it first before running.

We execute this file using the python3 query.

$ python 3 test.py

The following Tkinter screen named “ComboBox illustration” appears in the following:

When we click on the ComboBox triangle sign beneath the “Choose a Month” title, it shows the names of a month.

When we choose “July”, it is shown on the label and the dialogue alert and a message appears. Press Ok to continue.

Conclusion

This is all about using the Tkinter module of Python to create a comboBox in the GUI window. For this, we tried two simple yet different examples of Python to achieve the goal using the frame() function and the conventional way of creating the comboBox in the GUI. We incorporated the sample codes for both instances and made a few changes after the execution of the codes.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content