任何人都可以暗示我在这里做错了什么:
我尝试使用单选按钮的状态值来控制一个按钮的状态,即如果选择了 RadioButton1 ,则禁用浏览按钮否则,如果选择 RadioButton2 ,则启用浏览按钮。这是我的代码:
from Tkinter import *
root = Tk()
root.geometry('175x100')
root.title('GUI App')
root.resizable(0,0)
def selection_0():
val = var1.get()
if (str(val) == '0'):
print 'selection_0'
browseButton = Button(root, text="Browse", relief = 'raised', width=8, command=fun1, cursor='hand2', state=DISABLED).place(x=50,y=50)
radio_btn_state0 = ACTIVE
radio_btn_state1 = NORMAL
def selection_1():
val = var2.get()
if (str(val) == '1'):
print 'selection_1'
browseButton = Button(root, text="Browse", relief = 'raised', width=8, command=fun1, cursor='hand2', state=ACTIVE).place(x=50,y=50)
radio_btn_state1 = ACTIVE
radio_btn_state0 = NORMAL
def fun1():
pass
radio_btn_state0 = NORMAL
radio_btn_state1 = NORMAL
var1 = StringVar()
RadioButton1 = Radiobutton(root, text="Selection 1", variable=var1, value="0", command=selection_0, state=radio_btn_state0)
RadioButton1.place(x=2,y=10)
var2 = StringVar()
RadioButton2 = Radiobutton(root, text="Selection 2", variable=var2, value="1", command=selection_1,state=radio_btn_state1)
RadioButton2.place(x=82,y=10)
#Create the 'Browse' button
browseButton = Button(root, text="Browse", relief = 'raised', width=8, command=fun1, cursor='hand2', state=DISABLED).place(x=50,y=50)
root.mainloop()
但我在这里遇到两个问题:
答案 0 :(得分:0)
第一个问题出现了,因为我为每个单选按钮使用了单独的变量,即var1和var2,它们没有将它们分组到同一组中。解决方案是对两个单选按钮使用相同的变量:
var = IntVar()
RadioButton1 = Radiobutton(root, text="Selection 1", variable=var, value="1", command=selection_0).place(x=2,y=10)
RadioButton2 = Radiobutton(root, text="Selection 2", variable=var, value="2", command=selection_1).place(x=82,y=10)