在 Tkinter 中,如何管理使用 list() 创建的复选框的特定“功能”?

问题描述 投票:0回答:2

我有这些复选框,因此我想尽可能轻松地管理它们(之前我用 place(x=..., y=...) 创建了它们:

问题:对于每个复选框,我需要一个不同的功能。我所说的功能是指,例如,这个:

 Checkbox1 = tk.Checkbutton(self, text="aaa", variable=self.My_Checkbutton1, 
             onvalue=1, offvalue=0, height=1, bg="white", foreground='black', 
             activebackground="white", highlightthickness=0,                                
             command = lambda: clicked(self.My_Checkbutton1.get(), funcyion_My_Checkbutton1))
                                                                                                                                                       

所以我需要单独管理一个复选框,除了它的位置,它必须自动垂直定位

P.S:我需要上面的Checkbox1代码保持不变(使用tk.Checkbutton command=lamba...等),否则我的整个项目将会崩溃

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
cbframe = tk.Frame(root)
cbframe.pack(side="left", fill="y")

all_names = ("aaa", "bbb", "ccc", "ddd")
all_checkbuttons = {}

for name in all_names:
    myvar = tk.IntVar(root)

    combobox = tk.Checkbutton(cbframe, text=name, variable=myvar)
    combobox.pack(side="top", fill="x", anchor="w")
    
    #Checkbox 1 with its specific function
    Checkbox1 = tk.Checkbutton(self, 
    variable=self.My_Checkbutton1, 
    onvalue=1, offvalue=0, height=1, 
    bg="white", foreground='black', 
    activebackground="white", highlightthickness=0, 
    command = lambda: clicked(self.My_Checkbutton1.get(), function_My_Checkbutton1))

    #Checkbox 2 with its specific function
    Checkbox2 = tk.Checkbutton(self, 
    variable=self.My_Checkbutton2, 
    onvalue=1, offvalue=0, height=1, 
    bg="white", foreground='black', 
    activebackground="white", highlightthickness=0, 
    command = lambda: clicked(self.My_Checkbutton2.get(), function_My_Checkbutton2))

    #Checkbox 3 with its specific function
    ...

    if not i%2:
        sep = ttk.Separator(cbframe)
        sep.pack(side="top", fill="x")

tk.mainloop()

谢谢你

python python-3.x tkinter checkbox range
2个回答
0
投票

我处理此问题的方式会有所不同,我不会为专门分配给每个复选框的每个命令编写函数,而是使用这段代码迭代一个包含每个复选框名称的列表:

cb_function = [key for key, var in cb_name.items() if var.get()]
cb_function = [item.lower() for item in cb_function] 

然后有一个函数可以调用 GUI 代码之外的每个命令。

import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

cb_frame =tk.LabelFrame(frame, text="Check-boxes")
cb_frame.pack(side="left", fill="y")

options=["aaa", "bbb", "ccc", "ddd"]

vars = {}
for cb_name in options:
    var = StringVar(value="")
    vars[cb_name] = var
    heck_button= Checkbutton(
    cb_frame,
    text=cb_name,
    variable=var,
    onvalue=cb_name,
    offvalue="",
    height=1, 
    bg="white", 
    foreground='black',
    activebackground="white",
    highlightthickness=0,
    )

for widget in cb_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)


root.mainloop()

0
投票

将名称存储在列表中。迭代这些名称,创建小部件并将它们添加到字典中。

names = ("this", "that", "another one")
checkbuttons = {}
for name in names:
    …
    cb = tk.Checkbutton(…)
    checkbuttons[name] = cb
    …

然后您可以使用类似

checkbuttons[“that”]
的内容来引用特定的复选按钮

© www.soinside.com 2019 - 2024. All rights reserved.