Introduction
In this blog, I am going to create a Combo box widget and option capturing function in Python GUI application. When a user chooses a list of options in the combo box and clicks the "Submit" button, the action will capture the option and display the option in the button name.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
-
- import tkinter as tk
- from tkinter import ttk
- win = tk.Tk()
- win.title("Python GUI App")
- ttk.Label(win, text = "Choose the color:").grid(column = 0, row = 0)
- def click():
- action.configure(text = "chosen color is : " + numberChosen.get())
- action = ttk.Button(win, text = "Click", command = click)
- action.grid(column = 1, row = 1)
- number = tk.StringVar()
- numberChosen = ttk.Combobox(win, width = 12, textvariable = number)
- numberChosen['values'] = ("Red", "Blue", "Green")
- numberChosen.grid(column = 0, row = 1)
- numberChosen.current()
- win.mainloop()
About the code
- First, I am importing the tkinter module.
- Next, assign a class and variables and give the application title.
- Next, create a button and define the button function.
- Next, create a combo box and adding values in the code.
- Finally, I have started the Windows event loop by calling the mainloop method then execute the code.
Output