简体   繁体   English

如何在我的tkinter组合框的下拉列表中添加值?

[英]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.

相关问题 如何使用从我的SQLite数据库中检索的数据填充tkinter下拉框? - How do I populate tkinter drop-down box with data retrieved from my SQLite database? 每次按下“刷新”时,如何用新数据填充 tkinter 下拉框? - How do i populate tkinter drop-down box with new data everytime i pressed “refresh”? Tkinter - 如何用列表填充组合框? - Tkinter - How do I populate a combo box with a list? 引用 Tkinter 中的组合框值 - Referencing Combo Box Values in Tkinter 如何在Django中使用我的外键获得级联下拉选择框? - How do I get a cascading drop down selection box with my foreign keys in Django? 我可以在wxPython的列表控件中创建下拉组合框吗? - Can I create a drop down combo box within a list control in wxPython? 复选框/组合框的 Tkinter 下拉列表 - Tkinter drop down list of check-boxes/combo-boxes 如何将组合框中的选定项目添加到Jupyter小部件中的表中? - How do I add selected items from combo box to a table in Jupyter widgets? Python-Tkinter如何读取第二个下拉值 - Python - tkinter How to read the second drop down values Tkinter - 如何创建具有自动完成功能的组合框 - Tkinter - How to create a combo box with autocompletion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM