Python tkinter widgets Exercise: Create a Combobox with three options using tkinter module
Python tkinter widgets: Exercise-4 with Solution
Write a Python GUI program to create a Combobox with three options using tkinter module.
Sample Solution:
Python Code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
my_str_var = tk.StringVar()
my_combobox = ttk.Combobox(
root, textvariable = my_str_var,
values=["PHP", "Java", "Python"])
my_combobox.pack()
root.mainloop()
Explanation:
In the exercise above -
- import tkinter as tk - Import the Tkinter library (tkinter)
- from tkinter import ttk – Import the themed Tkinter library (ttk).
- root = tk.Tk() - Create the main Tkinter window (root).
- my_str_var = tk.StringVar() - Create a StringVar (my_str_var) to hold a string variable.
- my_combobox = ttk.Combobox(root, textvariable = my_str_var, values=["PHP", "Java", "Python"]) - Create a Combobox widget (my_combobox) using ttk.Combobox. Associate the StringVar (my_str_var) with the Combobox using textvariable. Set the values that can be selected in the Combobox.
- my_combobox.pack() - Display the Combobox within the main window.
- root.mainloop() - Start the Tkinter main loop to run the GUI application.
Sample Output:
Flowchart:
Python Code Editor:
Previous: Write a Python GUI program to create two buttons exit and hello using tkinter module.
Next: Write a Python GUI program to create a Checkbutton widget using tkinter module.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/tkinter/python-tkinter-widgets-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics