【问题标题】:TKinter ComboBox Variable IssueTKinter ComboBox 变量问题
【发布时间】:2022-10-23 00:38:51
【问题描述】:

我正在尝试使用 tkinter,更具体地说是 customtkinter,来创建一个组合框,并在第一个组合框中有一个变量来确定另一个组合框的选项是什么。我认为语法可能有点不同,因为它是 customtkinter 而不是标准的。看看是否有人可以看看我的代码。

这是该课程的一个片段:

# ---------------- HP Pool Option --------------#
        #HP pool values
        #Low
        hp_low_options = ["Random",
                        "5","10","15","20",
                        "25","30","35","40",
                        "45","50","55","60",
                        "65","70","75","80",
                        "85","90","95","100",
                        ]
        #Medium                
        hp_medium_options = ["Random",
                        "105","110","115","120",
                        "125","130","135","140",
                        "145","150","155","160",
                        "165","170","175","180",
                        "185","190","195","200",
                        ]
        #Large
        hp_high_options = ["Random",
                        "205","210","215","220",
                        "225","230","235","240",
                        "245","250","255","260",
                        "265","270","275","280",
                        "285","290","295","300",
                        ]
        #Extreme
        hp_extreme_options = ["Random",
                        "325","350","375","400",
                        "425","450","475","500",
                        "525","550","575","600",
                        "625","650","675","700",
                        "725","750","775","800",
                        ]

        #Create first combobox for pool type
        hp_pool_label = customtkinter.CTkLabel(master=self.frame_left, text="HP Option")
        hp_pool_label.grid(row=5,column=0)
        current_hp_pool = StringVar() #initialize variable
        hp_pool_combobox = customtkinter.CTkComboBox(master=self.frame_left, 
                                                    variable=current_hp_pool, #set variable in combobox
                                                    values=("Random",
                                                            "Low HP Pool", 
                                                            "Medium HP Pool", 
                                                            "High HP Pool", 
                                                            "Extreme HP Pool"))
        hp_pool_combobox.grid(row=5, column=1)

            #This is where the problems start I think 

            hp_pool_combobox.set("Random")  
            hp_pool_combobox.setvar("Test", current_hp_pool)
            current_hp_pool = hp_pool_combobox.current_value

            if current_hp_pool == "Random":
                hp_pool_selected = (hp_low_options,
                                    hp_medium_options,
                                    hp_high_options,
                                    hp_extreme_options)
            elif current_hp_pool == "Low HP Pool":
                hp_pool_selected = hp_low_options
            elif current_hp_pool == "Medium HP Pool":
                hp_pool_selected = hp_medium_options
            elif current_hp_pool == "High HP Pool":
                hp_pool_selected = hp_high_options
            elif current_hp_pool == "Extreme HP Pool":
                hp_pool_selected = hp_extreme_options
            
            hp_value_combobox = customtkinter.CTkComboBox(master=self.frame_left, values=hp_pool_selected)
            hp_value_combobox.grid(row=5, column=2)
            hp_value_combobox.set("Random")

I think creating an even of some sort is the answer so when the new option is selected the event triggers and changes the second combobox options.感谢您的时间和精力!

【问题讨论】:

  • GUI 不像 input() 那样工作 - 小部件不会等待您的选择,但它们只会通知 mainloop 它必须在窗口中显示的内容。 mainloop 之前的所有代码都会在你看到窗口之前执行。当您更改选项时,您必须分配将运行功能的事件 - 此功能应更新其他小部件。
  • 如果你使用StringVar()IntVar() 等,那么你必须使用.get() 来获得价值——即。 if current_hp_pool.get() == "Random":
  • 当您在Combobox github.com/TomSchimansky/CustomTkinter/wiki/CTkComboBox 中选择某些内容时,似乎这个Comboboxcomman=function_name(如Button)运行function_name()

标签: python tkinter combobox customtkinter


【解决方案1】:

一种方法是设置CTkComboboxcommand 选项,并根据回调内第一个组合框的选定值更新第二个组合框。

下面是一个例子:

import customtkinter as ctk

options = {
    'Low': ('5', '10'),
    'Medium': ('15', '20'),
    'High': ('25', '30'),
    'Extreme': ('35', '40')
}

def on_combo1_selected(value):
    values = ('Random',)
    if value == 'Random':
        for v in options.values():
            values += v
    else:
        values += options[value]
    combo2.configure(values=values)
    combo2.set('')

root = ctk.CTk()

var1 = ctk.StringVar()
combo1 = ctk.CTkComboBox(root, variable=var1, values=('Random',)+tuple(options.keys()), command=on_combo1_selected)
combo1.pack()

var2 = ctk.StringVar()
combo2 = ctk.CTkComboBox(root, variable=var2)
combo2.pack()

root.mainloop()

您可以将逻辑应用于您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多