We insert another column between the Entry widget and the Button widget using the grid layout manager. Here is the Python code:
- Start with the GUI_set_focus.py module and save it as GUI_combobox_widget.py.
- Change the button column to 2:
action = ttk.Button(win, text="Click Me!", command=click_me)
action.grid(column=2, row=1)
- Create a new ttk.Label widget:
ttk.Label(win, text="Choose a number:").grid(cloumn=1, row=0)
- Create a new ttk.Combobox widget:
number_chosen = ttk.Combobox(win, width=12, textvariable=number)
- Assign values to the Combobox widget:
number_chosen['value'] = (1, 2, 4, 42, 100)
- Place the Combobox widget into column 1:
number_chosen.grid(column=1, row=1)
number_chosen.current(0)
The preceding steps produce the following code (GUI_combobox_widget.py):
- Run the code.
The code, when added to the previous recipes, creates the following GUI. Note how, in line 43 in the preceding code, we assigned a tuple with default values to the combobox. These values then appear in the drop-down box. We can also change them if we like (by typing in different values when the application is running):
Let's go behind the scenes to understand the code better.