Intro
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 button name.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
- #Python Combobox Application
- import tkinter as tk
- from tkinter import ttk
- win = tk.Tk()
- #Application Name
- win.title("Python GUI App")
- #Label Creation
- ttk.Label(win, text="Choose the color:").grid(column=0,row=0)
- #Button Action
- def click():
- action.configure(text="chosen color is : "+ numberChosen.get())
- #button Creation
- action = ttk.Button(win, text="Click", command=click)
- action.grid(column=1,row=1)
- #Combobox Creation
- number= tk.StringVar()
- numberChosen= ttk.Combobox(win, width=12, textvariable=number)
- #Adding Values
- numberChosen['values']=("Red","Blue","Green")
- numberChosen.grid(column=0,row=1)
- numberChosen.current()
- #Calling Main()
- win.mainloop()
About the code
- First, I am importing the tkinter module.
- Next, assign a class and variables and give application title.
- Next, create button and define button function.
- Next, create 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