Using Combobox Widget In Python GUI Application

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
  1. #Python Combobox Application  
  2. import tkinter as tk  
  3. from tkinter import ttk  
  4. win = tk.Tk()# Application Name  
  5. win.title("Python GUI App")# Label Creation  
  6. ttk.Label(win, text = "Choose the color:").grid(column = 0, row = 0)# Button Action  
  7. def click():  
  8.     action.configure(text = "chosen color is : " + numberChosen.get())# button Creation  
  9.     action = ttk.Button(win, text = "Click", command = click)  
  10.     action.grid(column = 1, row = 1)# Combobox Creation  
  11. number = tk.StringVar()  
  12. numberChosen = ttk.Combobox(win, width = 12, textvariable = number)# Adding Values  
  13. numberChosen['values'] = ("Red""Blue""Green")  
  14. numberChosen.grid(column = 0, row = 1)  
  15. numberChosen.current()# Calling Main()  
  16. 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