Gray Out A Frame After Clicking A Radiobutton Tkinter
i have this code, i'm trying to make the the first frame to gray out once the user click one of the choices so that the user cannot change to other choice. but when i run it, it al
Solution 1:
At the beginning the value of specialistchoose.get()
is 0
, and the check for the buttons with an if
is not dynamic.
The Radiobutton
has a build in command
which is evaluated every time the button is pressed. You can use that to deactivate the buttons.
Like this:
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()
Side note to your code:
The if
statement in your code always evaluates True
in that case, as 'Cardiology' or 'Gastroenterology'
is True
You should use:
if specialistchoose.get() != 'Cardiology'or specialistchoose.get() != 'Gastroenterology'or specialistchoose.get() != 'Dermatology'or specialistchoose.get() != 'Psychiatry'or specialistchoose.get() != 'Dentist':
Post a Comment for "Gray Out A Frame After Clicking A Radiobutton Tkinter"