带有可执行示例的多个复选框选择中的简单问题。有没有自动识别多个复选框的方法?
Simple problem in multiple checkbox selection with executable example. Is there automatic way to recognize the plurality multiple selected checkboxes?
是否有自动识别多个 selected 复选框的方法?我不想手动创建多个 selection 组合。我想得到那个:
- 1/3 复选框:如果我单独 select 第一个或第三个框,我会在文本框中打印“确定”
- 2/3 复选框:如果我同时 select 第一个和第三个,我会在文本框中打印“ok”。但是只打印了一个“ok”,而不是两个“ok ok”(所以两个复选框对应一个 ok)
- 我不想在文本框中打印第二个复选框,因为我故意在上面写错了。
问题出在这个函数上。可能AND(或OR)不是我需要的,我猜,我不知道:
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func() and Button2_func() and Button3_func():
我想解决方案是为每种类型的多个 selection 案例创建其他条件,但我认为有一种更自动和更好的方法,无需创建 selection 有条件的案例,因为我将不得不添加几十个其他复选框,情况会变得复杂。
重要提示:请不要讽刺 True 和 False,因为 5 + 3 = 8 只是一个简单的示例,用于理解复选框的逻辑,我将不得不对其应用其他不同的代码。保持 return True 和 False 不变,不更改函数。
谁能帮我看看正确的代码?谢谢
import sqlite3
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.messagebox
from tkinter import messagebox
root = tk.Tk()
root.geometry("600x600")
root.configure(bg='white')
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
#CHECKBOX'S FUNCTIONS
def Button1_func():
if 5 + 3 == 8:
return True
else:
return False
def Button2_func():
if 5 + 3 == 7:
return True
else:
return False
def Button3_func():
if 5 + 3 == 8:
return True
else:
return False
#CHECKBOX
Button1 = Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button1_func)
Button1.place(x=10, y=36)
Button2 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button2_func)
Button2.place(x=10, y=66)
Button3 = Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button3_func)
Button3.place(x=10, y=146)
#BUTTON FUNCTION
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func () and Button2_func () and Button3_func ():
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=30, y=220)
#BUTTON
button = tk.Button(root, text="Ok", command= lambda: [aaa()])
button.pack()
root.mainloop()
更新
在代码中还有这个,我用它来保存和加载复选框:
chk_lst = []
chk_lst.extend([Checkbutton1,Checkbutton2, Checkbutton3])
已编辑以澄清问题
因为每个 Checkbutton
都有一个与之关联的独特功能,您将需要手动定义这些。现在,我已经用函数代替了。
您实际上不需要将函数传递给复选按钮,因为它们仅在您单击 Button
时才被评估。相反,您可以更改 aaa
,以便它为勾选的 Checkbuttons
创建一个函数结果列表。如果这些 return False
中的任何一个,都不要打印“OK”。但是如果他们都return True
,打印“OK”
import tkinter as tk
root = tk.Tk()
root.geometry("600x600")
# How many checkboxes you want
buttons = 5
button_eval = [0 for i in range(buttons)]
# If your functions are unique for each checkbox, you will need to define them here
def foo():
return True
def bar():
return False
my_functions = [foo, bar, foo, bar, foo] # You need to have 5 functions in this case for this to work
# Creating variables, buttons etc
x = [10, 10, 10, 10, 10]
y = [36, 66, 96, 126, 156]
checkbuttons_vars = [tk.IntVar() for _ in range(buttons)]
checkbuttons = [tk.Checkbutton(root, text=f"Checkbox {i+1}", variable=checkbuttons_vars[i], onvalue=1, offvalue=0) for i in range(buttons)]
for i, box in enumerate(checkbuttons):
box.place(x=x[i],y=y[i])
#BUTTON FUNCTION
def aaa():
function_results = [function() for i, function in enumerate(my_functions) if checkbuttons_vars[i].get()]
textbox.delete(1.0, "end")
if False not in function_results:
textbox.insert(1.0, "Ok")
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.pack()
#BUTTON
button = tk.Button(root, text="Ok", command=aaa)
button.pack()
root.mainloop()
您可以使用 set 来存储基于这些复选按钮状态的函数引用。然后在aaa()
里面执行set里面的所有函数,判断是否在文本框中输入OK:
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
funclist = set()
#CHECKBOX
Button1 = tk.Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton1.get(), Button1_func))
Button1.place(x=10, y=36)
Button2 = tk.Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton2.get(), Button2_func))
Button2.place(x=10, y=66)
Button3 = tk.Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton3.get(), Button3_func))
Button3.place(x=10, y=146)
#BUTTON FUNCTION
def aaa():
# if need to clear the text box, uncomment below line
#textbox.delete("1.0", "end")
if funclist and all(func() for func in funclist):
textbox.insert("end", "Ok")
是否有自动识别多个 selected 复选框的方法?我不想手动创建多个 selection 组合。我想得到那个:
- 1/3 复选框:如果我单独 select 第一个或第三个框,我会在文本框中打印“确定”
- 2/3 复选框:如果我同时 select 第一个和第三个,我会在文本框中打印“ok”。但是只打印了一个“ok”,而不是两个“ok ok”(所以两个复选框对应一个 ok)
- 我不想在文本框中打印第二个复选框,因为我故意在上面写错了。
问题出在这个函数上。可能AND(或OR)不是我需要的,我猜,我不知道:
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func() and Button2_func() and Button3_func():
我想解决方案是为每种类型的多个 selection 案例创建其他条件,但我认为有一种更自动和更好的方法,无需创建 selection 有条件的案例,因为我将不得不添加几十个其他复选框,情况会变得复杂。
重要提示:请不要讽刺 True 和 False,因为 5 + 3 = 8 只是一个简单的示例,用于理解复选框的逻辑,我将不得不对其应用其他不同的代码。保持 return True 和 False 不变,不更改函数。
谁能帮我看看正确的代码?谢谢
import sqlite3
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.messagebox
from tkinter import messagebox
root = tk.Tk()
root.geometry("600x600")
root.configure(bg='white')
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
#CHECKBOX'S FUNCTIONS
def Button1_func():
if 5 + 3 == 8:
return True
else:
return False
def Button2_func():
if 5 + 3 == 7:
return True
else:
return False
def Button3_func():
if 5 + 3 == 8:
return True
else:
return False
#CHECKBOX
Button1 = Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button1_func)
Button1.place(x=10, y=36)
Button2 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button2_func)
Button2.place(x=10, y=66)
Button3 = Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button3_func)
Button3.place(x=10, y=146)
#BUTTON FUNCTION
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func () and Button2_func () and Button3_func ():
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=30, y=220)
#BUTTON
button = tk.Button(root, text="Ok", command= lambda: [aaa()])
button.pack()
root.mainloop()
更新 在代码中还有这个,我用它来保存和加载复选框:
chk_lst = []
chk_lst.extend([Checkbutton1,Checkbutton2, Checkbutton3])
已编辑以澄清问题
因为每个 Checkbutton
都有一个与之关联的独特功能,您将需要手动定义这些。现在,我已经用函数代替了。
您实际上不需要将函数传递给复选按钮,因为它们仅在您单击 Button
时才被评估。相反,您可以更改 aaa
,以便它为勾选的 Checkbuttons
创建一个函数结果列表。如果这些 return False
中的任何一个,都不要打印“OK”。但是如果他们都return True
,打印“OK”
import tkinter as tk
root = tk.Tk()
root.geometry("600x600")
# How many checkboxes you want
buttons = 5
button_eval = [0 for i in range(buttons)]
# If your functions are unique for each checkbox, you will need to define them here
def foo():
return True
def bar():
return False
my_functions = [foo, bar, foo, bar, foo] # You need to have 5 functions in this case for this to work
# Creating variables, buttons etc
x = [10, 10, 10, 10, 10]
y = [36, 66, 96, 126, 156]
checkbuttons_vars = [tk.IntVar() for _ in range(buttons)]
checkbuttons = [tk.Checkbutton(root, text=f"Checkbox {i+1}", variable=checkbuttons_vars[i], onvalue=1, offvalue=0) for i in range(buttons)]
for i, box in enumerate(checkbuttons):
box.place(x=x[i],y=y[i])
#BUTTON FUNCTION
def aaa():
function_results = [function() for i, function in enumerate(my_functions) if checkbuttons_vars[i].get()]
textbox.delete(1.0, "end")
if False not in function_results:
textbox.insert(1.0, "Ok")
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.pack()
#BUTTON
button = tk.Button(root, text="Ok", command=aaa)
button.pack()
root.mainloop()
您可以使用 set 来存储基于这些复选按钮状态的函数引用。然后在aaa()
里面执行set里面的所有函数,判断是否在文本框中输入OK:
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
funclist = set()
#CHECKBOX
Button1 = tk.Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton1.get(), Button1_func))
Button1.place(x=10, y=36)
Button2 = tk.Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton2.get(), Button2_func))
Button2.place(x=10, y=66)
Button3 = tk.Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton3.get(), Button3_func))
Button3.place(x=10, y=146)
#BUTTON FUNCTION
def aaa():
# if need to clear the text box, uncomment below line
#textbox.delete("1.0", "end")
if funclist and all(func() for func in funclist):
textbox.insert("end", "Ok")