如何强制用户 select 至少一个复选按钮
How to force user to select at least one check button
我正在开发一个程序,希望用户 select 感兴趣,完成后按提交。如果至少选中一个按钮,我将如何只允许用户按下提交。
from tkinter import *
check = Tk()
check.title("Interests")
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
submit_btn = Button(check, text = "Submit", command = lambda: check.destroy())
C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
submit_btn.pack()
check.mainloop()
if CheckVar1.get():
#dosomething
if CheckVar2.get():
#dosomething
if CheckVar3.get():
#dosomething
if CheckVar4.get():
#dosomething
if CheckVar5.get():
#dosomething
为了使这更容易处理,您可以创建一个兴趣列表,然后使用它们循环显示所有内容。这也将使将来添加内容变得更加容易,例如:
from tkinter import *
check = Tk()
check.title("Interests")
def destroy():
if 1 in [vars[interest].get() for interest in interests]:
check.destroy()
interests = ["Horror", "Action", "Documentary", "Science fiction", "Comedy"]
vars = {interest:IntVar() for interest in interests}
boxes = [Checkbutton(check, text = interest, variable = vars[interest], \
onvalue = 1, offvalue = 0, height=1, \
width = 20) for interest in interests]
submit_btn = Button(check, text = "Submit", command = destroy)
tmp = [box.pack() for box in boxes]
submit_btn.pack()
在销毁 window.
之前,您需要添加一个处理函数来检查是否至少有一个框被选中
您可以使用以下逻辑轻松地做到这一点:
from tkinter import *
from tkinter import messagebox
check = Tk()
check.title("Interests")
CheckVar1 = BooleanVar()
CheckVar2 = BooleanVar()
CheckVar3 = BooleanVar()
CheckVar4 = BooleanVar()
CheckVar5 = BooleanVar()
C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
def submitHandle():
anyChecked = CheckVar1.get() | CheckVar2.get() | CheckVar3.get(
) | CheckVar4.get() | CheckVar5.get()
if anyChecked:
check.destroy()
else:
messagebox.showerror("Error", "Please check at least one.")
submit_btn = Button(check, text="Submit", command=lambda: submitHandle())
C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
submit_btn.pack()
check.mainloop()
P.S。请注意,您应该使用 BooleanVar
而不是 IntVar
作为开关复选框。另外,考虑 adding a check on window 关闭。
我正在开发一个程序,希望用户 select 感兴趣,完成后按提交。如果至少选中一个按钮,我将如何只允许用户按下提交。
from tkinter import *
check = Tk()
check.title("Interests")
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
submit_btn = Button(check, text = "Submit", command = lambda: check.destroy())
C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
submit_btn.pack()
check.mainloop()
if CheckVar1.get():
#dosomething
if CheckVar2.get():
#dosomething
if CheckVar3.get():
#dosomething
if CheckVar4.get():
#dosomething
if CheckVar5.get():
#dosomething
为了使这更容易处理,您可以创建一个兴趣列表,然后使用它们循环显示所有内容。这也将使将来添加内容变得更加容易,例如:
from tkinter import *
check = Tk()
check.title("Interests")
def destroy():
if 1 in [vars[interest].get() for interest in interests]:
check.destroy()
interests = ["Horror", "Action", "Documentary", "Science fiction", "Comedy"]
vars = {interest:IntVar() for interest in interests}
boxes = [Checkbutton(check, text = interest, variable = vars[interest], \
onvalue = 1, offvalue = 0, height=1, \
width = 20) for interest in interests]
submit_btn = Button(check, text = "Submit", command = destroy)
tmp = [box.pack() for box in boxes]
submit_btn.pack()
在销毁 window.
之前,您需要添加一个处理函数来检查是否至少有一个框被选中您可以使用以下逻辑轻松地做到这一点:
from tkinter import *
from tkinter import messagebox
check = Tk()
check.title("Interests")
CheckVar1 = BooleanVar()
CheckVar2 = BooleanVar()
CheckVar3 = BooleanVar()
CheckVar4 = BooleanVar()
CheckVar5 = BooleanVar()
C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
onvalue = 1, offvalue = 0, height=1, \
width = 20)
def submitHandle():
anyChecked = CheckVar1.get() | CheckVar2.get() | CheckVar3.get(
) | CheckVar4.get() | CheckVar5.get()
if anyChecked:
check.destroy()
else:
messagebox.showerror("Error", "Please check at least one.")
submit_btn = Button(check, text="Submit", command=lambda: submitHandle())
C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
submit_btn.pack()
check.mainloop()
P.S。请注意,您应该使用 BooleanVar
而不是 IntVar
作为开关复选框。另外,考虑 adding a check on window 关闭。