【发布时间】:2020-10-20 00:43:06
【问题描述】:
我有一个 tkinter GUI,里面有两个 CheckButton。它们用于“或”和“与”。当OR按钮被勾选时,变量andCond为False,当AND按钮被勾选时,变量andCond为True。
from Tkinter import *
import pdb
import tkinter as tk
global andCond
root = tk.Tk()
color = '#aeb3b0'
def check():
global andCond
if checkVar.get():
print('OR')
andCond = not(checkVar.get())
print(andCond)
else:
print('AND')
andCond = not(checkVar.get())
print(andCond)
checkVar = tk.IntVar()
checkVar.set(True)
checkBoxAND = tk.Checkbutton(root, text = "AND", variable = checkVar, onvalue = 0, offvalue = 1, command = check, width =19, bg = '#aeb3b0')
checkBoxAND.place(relx = 0.22, rely = 0.46)
checkBoxOR = tk.Checkbutton(root, text = "OR", variable = checkVar, onvalue = 1, offvalue = 1, command = check, width =19, bg = '#aeb3b0')
checkBoxOR.place(relx = 0.22, rely = 0.36)
andCond = not(checkVar.get())
print(andCond)
root.mainloop()
这一切都按需要工作,除了有一件小事我无法解决。当 OR 按钮被选中时,如果我再次点击它,什么也没有发生(这就是我想要的) 但是当 AND 按钮被选中时,我再次点击它,按钮切换并且 OR 现在被选中。
如何防止这种情况发生?
谢谢
R
【问题讨论】:
-
您是否希望用户只选择“OR”或“AND”之一,而不是两者?如果是这种情况,则 Checkbutton 是不正确的小部件。单选按钮专为独家选择而设计。