TKINTER COMBOBOX变量问题

发布于 2025-02-06 16:27:14 字数 3572 浏览 2 评论 0原文

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

这是班级的snipit:

# ---------------- 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")

我认为创建某种形式的偶数是答案,因此当选择新选项时,事件触发器并更改第二个ComboBox选项。感谢您的时间和精力!

I am trying to use tkinter, more specifically customtkinter, to create a combobox and have a variable in the first combobox determine what another combobox's options are. I think the syntax maybe a little different since it is customtkinter rather than standard. Seeing if anyone can take a look at my code.

Here is s snipit of the class:

# ---------------- 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. Thank you for your time and effort!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2025-02-13 16:27:15

一种方法是设置命令 ctkcombobox的选项,并根据回调中的第一个Combobox的选定值更新第二个ComboBox。

以下是一个示例:

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()

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

One of the way is to set command option of CTkCombobox and update the second combobox based on the selected value of first combobox inside the callback.

Below is an example:

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()

You can apply the logic to your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文