[英]How do I add values in the drop down of my tkinter combo box?
I am creating a multi-row GUI for booking details. 我正在创建一个用于预订详细信息的多行GUI。 I have already imported the tkinter and written code for the other text fields but I cannot put values in my combo box!
我已经为其他文本字段导入了tkinter和编写的代码但是我无法在我的组合框中输入值! I get it to show up in the window but it has no values in it!!
我把它显示在窗口中,但它没有值! Where am I going wrong?
我哪里错了?
I have tried using another code but when I ran the code, two windows popped up - one for the total price, flight host, etc and one for the combo box 我尝试过使用其他代码但是当我运行代码时,会弹出两个窗口 - 一个用于总价,飞行主机等,另一个用于组合框
tk.Label(form_frame, text= "Go To", font=('arial', 10),
width=20, anchor="e", bd=1,
pady=10, padx=10).grid(row=4, column=0)
comboExample = ttk.Combobox(
values=[
"Customer Details",
"Flight Details"])
combo_prod_cat = ttk.Combobox(form_frame, state="normal", width=10)
combo_prod_cat.grid(row=4, column=1, sticky="w")
I am expecting the window to pop up as this: https://imgur.com/jVTtj5d 我期待窗口弹出如下: https : //imgur.com/jVTtj5d
but with the values Customer Details and Flight Details in the drop down 但是下拉列表中的客户详细信息和航班详细信息值
I think that you need 'OptionMenu()' instead of 'ComboBox()'. 我认为你需要'OptionMenu()'而不是'ComboBox()'。 A short example below:
以下简短示例:
# your options
values = ['-', 'Customer Details', 'Flight Details']
# variable which holds the current selected option. It none selected it's '-'.
dd_option = StringVar(form_frame)
dd_option.set('-')
dropdown = OptionMenu(form_frame, dd_option, *values)
dropdown.grid(<your element placement>)
To trace the selection you can use s.th. 要跟踪选择,您可以使用s.th. like this:
像这样:
def value_changed(*args):
# exmaple
print(dd_option.get())
dd_option.trace('w', value_change)
Here is a full example of option menu: https://pythonspot.com/tk-dropdown-example/ 以下是选项菜单的完整示例: https : //pythonspot.com/tk-dropdown-example/
Set the values parameter. 设置值参数。
chosen_var = StringVar(form_frame)
combo_prod_cat = ttk.Combobox(form_frame, state="normal", width=10, values=["Choose", "Customer Details", "Flight Details"], textvariable=chosen_var, width=20)
combo_prod_cat.current(0)
combo_prod_cat.grid(row=4, column=1, sticky="w")
That works on my machine. 这适用于我的机器。 And have you noticed that you're not specifying values in
combo_prod_cat
? 您是否注意到您没有在
combo_prod_cat
指定值? Only in comboExample
? 仅在
comboExample
?
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.