Using Combobox Widget In Python GUI Application

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

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

 
 
Ebook Download
View all
Learn
View all