Tkinter Combobox Tutorial
Combobox can be translated to combo box, which is a Widget control for tkinter.ttk. Its features are similar to OptionMenu, which can be said to be a combination of Entry and dropdown menu. Its construction method is as follows.
Combobox(master, options)
Commonly used options parameters are as follows.
(1) textvariable
: you can set the variable value of Combobox.
(2) v
: the option content of Combobox, the content exists as a tuple.
Example#1
In the Combobox( ) constructor method, you can use the value parameter to build the option content, the following example code will build a Combobox.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x120")
var = StringVar()
cb = Combobox(
root,
textvariable=var,
value=("Python","Java","C#","C")
)
cb.pack(pady=10)
root.mainloop()
Output:
In fact, when designing the above program, if there are many options, the parameter value of Combobox( ) method is generally handled independently outside of Combobox( ), you can refer to the following example.
Example#2
Treat the Combobox options independently.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x120")
var = StringVar()
cb = Combobox(root,textvariable=var) # Create Combobox
cb["value"] = ("Python","Java","C#","C","C++") # Set option content
cb.pack(pady=10)
root.mainloop()
Output: