So here's my code
root = Tk()
# 1st frame
frame_1 = LabelFrame(root)
frame_1.pack()
#var
var = IntVar()
var.set(0)
test = ['test1', 'test2', 'test3', 'test4']
# radio button
radio_1 = Radiobutton(frame_1, text = test[0], value = 1, variable = var)
radio_1.pack()
radio_2 = Radiobutton(frame_1, text = test[1], value = 2, variable = var)
radio_2.pack()
radio_3 = Radiobutton(frame_1, text = test[2], value = 3, variable = var)
radio_3.pack()
# 2nd frame
frame_2 = LabelFrame(root)
frame_2.pack()
# buttons
button_1 = Button(frame_2, text = test[0], state = DISABLED)
button_1.pack()
button_2 = Button(frame_2, text = test[1], state = DISABLED)
button_2.pack()
button_3 = Button(frame_2, text = test[2], state = DISABLED)
button_3.pack()
button_4 = Button(frame_2, text = test[3], state = DISABLED)
button_4.pack()
root.mainloop()
What I'm trying to do is have the buttons enabled when the radio buttons are selected. Here's what I tried.
def changeState():
if var.get() != 0:
button_1['state'] = NORMAL
button_2['state'] = NORMAL
button_3['state'] = NORMAL
button_4['state'] = NORMAL
changeState()
I believe this only sets it initially, and when I select a radio button or change it, it doesn't update. I think I can use command inside the buttons but don't understand how to go about it.
CodePudding user response:
I added four function., Then in Radiobutton
, I added command=changeStat
e for each Radiobuuton
.
Try this:
from tkinter import *
root = Tk()
# 1st frame
frame_1 = LabelFrame(root)
frame_1.pack()
#var
var = IntVar()
var.set(0)
test = ['test1', 'test2', 'test3', 'test4']
def changeState1():
if var.get() !=0:
button_1['state'] = NORMAL
button_2['state'] = DISABLED
button_3['state'] = DISABLED
button_4['state'] = DISABLED
def changeState2():
if var.get() !=0:
button_1['state'] = DISABLED
button_2['state'] = NORMAL
button_3['state'] = DISABLED
button_4['state'] = DISABLED
def changeState3():
if var.get() !=0:
button_1['state'] = DISABLED
button_2['state'] = DISABLED
button_3['state'] = NORMAL
button_4['state'] = DISABLED
def changeState4():
if var.get() !=0:
button_1['state'] = DISABLED
button_2['state'] = DISABLED
button_3['state'] = DISABLED
button_4['state'] = NORMAL
# radio button
radio_1 = Radiobutton(frame_1, text = test[0], value = 1,command=changeState1, variable = var)
radio_1.pack()
radio_2 = Radiobutton(frame_1, text = test[1], value = 2, command=changeState2, variable = var)
radio_2.pack()
radio_3 = Radiobutton(frame_1, text = test[2], value = 3,command=changeState3, variable = var)
radio_3.pack()
radio_4= Radiobutton(frame_1, text = test[2], value = 3,command=changeState4, variable = var)
radio_4.pack()
# 2nd frame
frame_2 = LabelFrame(root)
frame_2.pack()
# buttons
button_1 = Button(frame_2, text = test[0], state = DISABLED)
button_1.pack()
button_2 = Button(frame_2, text = test[1], state = DISABLED)
button_2.pack()
button_3 = Button(frame_2, text = test[2], state = DISABLED)
button_3.pack()
button_4 = Button(frame_2, text = test[3], state = DISABLED)
button_4.pack()
root.mainloop()
changeState()