Python 带有 tkinter 和 If 条件的复选框
Python Checkboxes with tkinter and If Condition
我想创建某种优先级生成器。这保持非常简单。我有 8 个不同的问题,并根据答案输出优先级。到目前为止,这也很有效。
但是,我有一个错误,即我的“其他”条件似乎不起作用。
例子:
如果选择问题1和问题8,那么Else条件应该输出“不可能”。不幸的是,这不起作用。
我已经定义了可能性,定义规则之外的所有内容都应该 运行 进入 else 条件。
这是代码:
from tkinter import *
ws = Tk()
ws.title('Prio Guide')
ws.geometry('600x680')
ws.configure(bg="white")
text = StringVar()
label = Label(ws,
text='empty',
textvariable=text,
bg="green")
prio1 = StringVar()
prio1_text = Label(ws, text='empty', textvariable=prio1, bg="white")
prio1.set("Prio 1 - Description")
prio2 = StringVar()
prio2_text = Label(ws, text='empty', textvariable=prio2, bg="white")
prio2.set("Prio 2- Description")
prio3 = StringVar()
prio3_text = Label(ws, text='empty', textvariable=prio3, bg="white")
prio3.set("Prio 3- Description")
prio4 = StringVar()
prio4_text = Label(ws, text='empty', textvariable=prio4, bg="white")
prio4.set("Prio 4- Description")
# define the Prio
def prio():
if cb.get() == 1 or cb1.get() == 1 or cb2.get() == 1 or cb.get() & cb1.get() == 1 or cb2.get() & cb1.get() == 1 or cb.get() & cb1.get() & cb2.get() == 1:
text.set("Prio 1")
elif cb3.get() == 1 or cb4.get() == 1 or cb3.get() & cb4.get() == 1:
text.set("Prio 2")
elif cb5.get() == 1 or cb5.get() & cb6.get() == 1:
text.set("Prio 3")
elif cb7.get() == 1 or cb6.get() & cb7.get() == 1:
text.set("Prio 4")
else:
text.set("not possible")
# create IntVar fot the value of checkboxes
cb = IntVar()
cb1 = IntVar()
cb2 = IntVar()
cb3 = IntVar()
cb4 = IntVar()
cb5 = IntVar()
cb6 = IntVar()
cb7 = IntVar()
# create checkbutton
Checkbutton(ws,
bg="white",
text="q1",
variable=cb,
onvalue=1,
offvalue=0,
command=prio, ).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q2",
variable=cb1,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q3",
variable=cb2,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q4",
variable=cb3,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q5",
variable=cb4,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q6",
variable=cb5,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q7",
variable=cb6,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q8",
variable=cb7,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
label.pack(anchor=S)
prio1_text.pack(anchor=S, padx=(100, 10))
prio2_text.pack(anchor=S, padx=(100, 10))
prio3_text.pack(anchor=S, padx=(100, 10))
prio4_text.pack(anchor=S, padx=(100, 10))
ws.mainloop()
感谢您的帮助!
你的“或”有问题,只要你有一个或条件等于真,它就不会return你的其他条件。
我想提出一个棘手的解决方案,即使用模式来获得优先级。
from tkinter import *
ws = Tk()
ws.title('Prio Guide')
ws.geometry('600x680')
ws.configure(bg="white")
text = StringVar()
label = Label(ws,
text='empty',
textvariable=text,
bg="green")
prio1 = StringVar()
prio1_text = Label(ws, text='empty', textvariable=prio1, bg="white")
prio1.set("Prio 1 - Description")
prio2 = StringVar()
prio2_text = Label(ws, text='empty', textvariable=prio2, bg="white")
prio2.set("Prio 2- Description")
prio3 = StringVar()
prio3_text = Label(ws, text='empty', textvariable=prio3, bg="white")
prio3.set("Prio 3- Description")
prio4 = StringVar()
prio4_text = Label(ws, text='empty', textvariable=prio4, bg="white")
prio4.set("Prio 4- Description")
# define the Prio
def getCbValue():
return "".join(str(x) for x in [cb.get(), cb1.get(), cb2.get(), cb3.get(), cb4.get(), cb5.get(), cb6.get(), cb7.get()])
def getPrio(cbValue):
prioConf = {'Prio 1': ['10000000', '01000000', '00100000', '11000000', '01100000'],
'Prio 2': ['00010000', '00001000', '00011000'],
'Prio 3': ['00000100', '00000110'],
'Prio 4': ['00000001', '00000011']}
key_list = list(prioConf.keys())
value_list = list(prioConf.values())
for i, d in enumerate(value_list):
if cbValue in d:
return key_list[i]
def prio():
prio = getPrio(getCbValue())
if prio:
text.set(prio)
else:
text.set("not possible")
# create IntVar fot the value of checkboxes
cb = IntVar()
cb1 = IntVar()
cb2 = IntVar()
cb3 = IntVar()
cb4 = IntVar()
cb5 = IntVar()
cb6 = IntVar()
cb7 = IntVar()
# create checkbutton
Checkbutton(ws,
bg="white",
text="q1",
variable=cb,
onvalue=1,
offvalue=0,
command=prio,).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q2",
variable=cb1,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q3",
variable=cb2,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q4",
variable=cb3,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q5",
variable=cb4,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q6",
variable=cb5,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q7",
variable=cb6,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q8",
variable=cb7,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
label.pack(anchor=S)
prio1_text.pack(anchor=S, padx=(100, 10))
prio2_text.pack(anchor=S, padx=(100, 10))
prio3_text.pack(anchor=S, padx=(100, 10))
prio4_text.pack(anchor=S, padx=(100, 10))
ws.mainloop()
您可以检查有 2 个新方法“getCbValue”和“getPrio”。
我还重构了你的 if-else,所以只要模式匹配,它就会 return 优先级,如果不匹配,它会 return 错误消息“不可能”。
我想创建某种优先级生成器。这保持非常简单。我有 8 个不同的问题,并根据答案输出优先级。到目前为止,这也很有效。 但是,我有一个错误,即我的“其他”条件似乎不起作用。 例子: 如果选择问题1和问题8,那么Else条件应该输出“不可能”。不幸的是,这不起作用。 我已经定义了可能性,定义规则之外的所有内容都应该 运行 进入 else 条件。 这是代码:
from tkinter import *
ws = Tk()
ws.title('Prio Guide')
ws.geometry('600x680')
ws.configure(bg="white")
text = StringVar()
label = Label(ws,
text='empty',
textvariable=text,
bg="green")
prio1 = StringVar()
prio1_text = Label(ws, text='empty', textvariable=prio1, bg="white")
prio1.set("Prio 1 - Description")
prio2 = StringVar()
prio2_text = Label(ws, text='empty', textvariable=prio2, bg="white")
prio2.set("Prio 2- Description")
prio3 = StringVar()
prio3_text = Label(ws, text='empty', textvariable=prio3, bg="white")
prio3.set("Prio 3- Description")
prio4 = StringVar()
prio4_text = Label(ws, text='empty', textvariable=prio4, bg="white")
prio4.set("Prio 4- Description")
# define the Prio
def prio():
if cb.get() == 1 or cb1.get() == 1 or cb2.get() == 1 or cb.get() & cb1.get() == 1 or cb2.get() & cb1.get() == 1 or cb.get() & cb1.get() & cb2.get() == 1:
text.set("Prio 1")
elif cb3.get() == 1 or cb4.get() == 1 or cb3.get() & cb4.get() == 1:
text.set("Prio 2")
elif cb5.get() == 1 or cb5.get() & cb6.get() == 1:
text.set("Prio 3")
elif cb7.get() == 1 or cb6.get() & cb7.get() == 1:
text.set("Prio 4")
else:
text.set("not possible")
# create IntVar fot the value of checkboxes
cb = IntVar()
cb1 = IntVar()
cb2 = IntVar()
cb3 = IntVar()
cb4 = IntVar()
cb5 = IntVar()
cb6 = IntVar()
cb7 = IntVar()
# create checkbutton
Checkbutton(ws,
bg="white",
text="q1",
variable=cb,
onvalue=1,
offvalue=0,
command=prio, ).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q2",
variable=cb1,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q3",
variable=cb2,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q4",
variable=cb3,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q5",
variable=cb4,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q6",
variable=cb5,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q7",
variable=cb6,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q8",
variable=cb7,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
label.pack(anchor=S)
prio1_text.pack(anchor=S, padx=(100, 10))
prio2_text.pack(anchor=S, padx=(100, 10))
prio3_text.pack(anchor=S, padx=(100, 10))
prio4_text.pack(anchor=S, padx=(100, 10))
ws.mainloop()
感谢您的帮助!
你的“或”有问题,只要你有一个或条件等于真,它就不会return你的其他条件。
我想提出一个棘手的解决方案,即使用模式来获得优先级。
from tkinter import *
ws = Tk()
ws.title('Prio Guide')
ws.geometry('600x680')
ws.configure(bg="white")
text = StringVar()
label = Label(ws,
text='empty',
textvariable=text,
bg="green")
prio1 = StringVar()
prio1_text = Label(ws, text='empty', textvariable=prio1, bg="white")
prio1.set("Prio 1 - Description")
prio2 = StringVar()
prio2_text = Label(ws, text='empty', textvariable=prio2, bg="white")
prio2.set("Prio 2- Description")
prio3 = StringVar()
prio3_text = Label(ws, text='empty', textvariable=prio3, bg="white")
prio3.set("Prio 3- Description")
prio4 = StringVar()
prio4_text = Label(ws, text='empty', textvariable=prio4, bg="white")
prio4.set("Prio 4- Description")
# define the Prio
def getCbValue():
return "".join(str(x) for x in [cb.get(), cb1.get(), cb2.get(), cb3.get(), cb4.get(), cb5.get(), cb6.get(), cb7.get()])
def getPrio(cbValue):
prioConf = {'Prio 1': ['10000000', '01000000', '00100000', '11000000', '01100000'],
'Prio 2': ['00010000', '00001000', '00011000'],
'Prio 3': ['00000100', '00000110'],
'Prio 4': ['00000001', '00000011']}
key_list = list(prioConf.keys())
value_list = list(prioConf.values())
for i, d in enumerate(value_list):
if cbValue in d:
return key_list[i]
def prio():
prio = getPrio(getCbValue())
if prio:
text.set(prio)
else:
text.set("not possible")
# create IntVar fot the value of checkboxes
cb = IntVar()
cb1 = IntVar()
cb2 = IntVar()
cb3 = IntVar()
cb4 = IntVar()
cb5 = IntVar()
cb6 = IntVar()
cb7 = IntVar()
# create checkbutton
Checkbutton(ws,
bg="white",
text="q1",
variable=cb,
onvalue=1,
offvalue=0,
command=prio,).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q2",
variable=cb1,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q3",
variable=cb2,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q4",
variable=cb3,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q5",
variable=cb4,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q6",
variable=cb5,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q7",
variable=cb6,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
Checkbutton(ws,
bg="white",
text="q8",
variable=cb7,
onvalue=1,
offvalue=0,
command=prio).pack(anchor=W, padx=(100, 10))
label.pack(anchor=S)
prio1_text.pack(anchor=S, padx=(100, 10))
prio2_text.pack(anchor=S, padx=(100, 10))
prio3_text.pack(anchor=S, padx=(100, 10))
prio4_text.pack(anchor=S, padx=(100, 10))
ws.mainloop()
您可以检查有 2 个新方法“getCbValue”和“getPrio”。
我还重构了你的 if-else,所以只要模式匹配,它就会 return 优先级,如果不匹配,它会 return 错误消息“不可能”。