我有这个代码,一旦用户点击其中一个选项,我试图让第一帧变灰,这样用户就无法改变到其他选择。但是当我运行它时,它甚至在用户选择之前就已经灰显了框架。我还想让第二帧变灰,直到用户点击/选择第一帧。有人可以帮忙吗?
from Tkinter import *
def onclick():
pass
import tkMessageBox
import ttk
root = Tk()
root.title("Pantai Hospital")
root.geometry("200x200")
Label(root, text = "Welcome to Pantai Hospital!").grid()
f1 = Frame(root, width = 350, height = 110)
f2 = Frame(f1, relief = GROOVE, borderwidth = 2)
l9 = Label(f2, text = "Choose your specialist:")
l9.grid()
specialistchoose = IntVar()
r1 = Radiobutton (f2, text = "Cardiology", variable = specialistchoose, value = 1)
r1.grid(row = 1, column = 0, stick = W)
r2 = Radiobutton (f2, text = "Gastroenterology", variable = specialistchoose, value = 2)
r2.grid(row = 1, column = 1,stick = W )
r3 = Radiobutton (f2, text = "Dermatology", variable = specialistchoose, value = 3)
r3.grid (row = 1, column = 2,stick = W )
r4 = Radiobutton (f2, text = "Psychiatry", variable = specialistchoose, value = 4)
r4.grid (row = 3, column = 0,stick = W )
r5 = Radiobutton (f2, text = "Dentist", variable = specialistchoose, value = 5)
r5.grid(row = 3, column = 1,stick = W )
f2.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f1, text = "Specialist").place(relx = .06, rely = 0.125, anchor = W)
f1.grid()
if specialistchoose.get() <> 'Cardiology' or 'Gastroenterology' or 'Dermatology' or 'Psychiatry' or 'Dentist' :
for child in f2.winfo_children():
child.configure(state = 'disable')
f3 = Frame(root, width = 350, height = 110)
f4 = Frame(f3, relief = GROOVE, borderwidth = 2)
l15 = Label (f4, text = "Choose your preferred day:")
l15.grid(columnspan = 2, sticky = W)
day = IntVar()
cb1 = Checkbutton(f4, text = "Monday", variable = day, onvalue = 1, offvalue = 0)
cb1.grid(row = 4, column = 0,stick = W )
cb2 = Checkbutton(f4, text = "Tuesday", variable = day, onvalue = 2, offvalue = 0)
cb2.grid(row = 4, column = 1,stick = W )
cb3 = Checkbutton(f4, text = "Wednesday", variable = day, onvalue = 3, offvalue = 0)
cb3.grid(row = 4, column = 2, stick = W)
cb4 = Checkbutton(f4, text = "Thursday", variable = day, onvalue = 4, offvalue = 0)
cb4.grid(row = 4, column = 3,stick = W )
cb5 = Checkbutton (f4, text = "Friday", variable = day, onvalue = 5, offvalue = 0)
cb5.grid(row = 5, column = 0, stick = W )
cb6 = Checkbutton(f4, text = "Saturday", variable = day, onvalue = 6, offvalue = 0)
cb6.grid(row = 5, column = 1, stick = W )
cb7 = Checkbutton (f4, text = "Sunday", variable = day, onvalue = 7, offvalue = 0)
cb7.grid(row = 5, column = 2, stick = W )
f4.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f3, text = "Day").place(relx = .06, rely = 0.125, anchor = W)
f3.grid()
root.mainloop()
答案 0 :(得分:1)
在开始时,specialistchoose.get()
的值为0
,并且对if
按钮的检查不是动态的。
Radiobutton
内置command
,每按一次按钮就会对其进行评估。您可以使用它来停用按钮。
像这样:
from Tkinter import *
def func():
for child in f2.winfo_children():
child.configure(state = 'disable')
import tkMessageBox
import ttk
root = Tk()
root.title("Pantai Hospital")
root.geometry("200x200")
Label(root, text = "Welcome to Pantai Hospital!").grid()
f1 = Frame(root, width = 350, height = 110)
f2 = Frame(f1, relief = GROOVE, borderwidth = 2)
l9 = Label(f2, text = "Choose your specialist:")
l9.grid()
specialistchoose = IntVar()
r1 = Radiobutton (f2, text = "Cardiology", variable = specialistchoose, value = 1, command=func)
r1.grid(row = 1, column = 0, stick = W)
r2 = Radiobutton (f2, text = "Gastroenterology", variable = specialistchoose, value = 2, command=func)
r2.grid(row = 1, column = 1,stick = W )
r3 = Radiobutton (f2, text = "Dermatology", variable = specialistchoose, value = 3, command=func)
r3.grid (row = 1, column = 2,stick = W )
r4 = Radiobutton (f2, text = "Psychiatry", variable = specialistchoose, value = 4, command=func)
r4.grid (row = 3, column = 0,stick = W )
r5 = Radiobutton (f2, text = "Dentist", variable = specialistchoose, value = 5, command=func)
r5.grid(row = 3, column = 1,stick = W )
f2.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f1, text = "Specialist").place(relx = .06, rely = 0.125, anchor = W)
f1.grid()
f3 = Frame(root, width = 350, height = 110)
f4 = Frame(f3, relief = GROOVE, borderwidth = 2)
l15 = Label (f4, text = "Choose your preferred day:")
l15.grid(columnspan = 2, sticky = W)
day = IntVar()
cb1 = Checkbutton(f4, text = "Monday", variable = day, onvalue = 1, offvalue = 0)
cb1.grid(row = 4, column = 0,stick = W )
cb2 = Checkbutton(f4, text = "Tuesday", variable = day, onvalue = 2, offvalue = 0)
cb2.grid(row = 4, column = 1,stick = W )
cb3 = Checkbutton(f4, text = "Wednesday", variable = day, onvalue = 3, offvalue = 0)
cb3.grid(row = 4, column = 2, stick = W)
cb4 = Checkbutton(f4, text = "Thursday", variable = day, onvalue = 4, offvalue = 0)
cb4.grid(row = 4, column = 3,stick = W )
cb5 = Checkbutton (f4, text = "Friday", variable = day, onvalue = 5, offvalue = 0)
cb5.grid(row = 5, column = 0, stick = W )
cb6 = Checkbutton(f4, text = "Saturday", variable = day, onvalue = 6, offvalue = 0)
cb6.grid(row = 5, column = 1, stick = W )
cb7 = Checkbutton (f4, text = "Sunday", variable = day, onvalue = 7, offvalue = 0)
cb7.grid(row = 5, column = 2, stick = W )
f4.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f3, text = "Day").place(relx = .06, rely = 0.125, anchor = W)
f3.grid()
root.mainloop()
您的代码附注:
在这种情况下,代码中的if
语句始终会评估True
,'Cardiology' or 'Gastroenterology'
为True
您应该使用:
if specialistchoose.get() != 'Cardiology' or specialistchoose.get() != 'Gastroenterology' or specialistchoose.get() != 'Dermatology' or specialistchoose.get() != 'Psychiatry' or specialistchoose.get() != 'Dentist':