我用Tkinter制作了一个带有一些按钮的简单窗口。一个按钮在开始时处于禁用状态,但我希望它在按下另一个按钮后再次变为正常状态。那么如何在代码运行时更改按钮的属性?
from tkinter import *
def com():
print ('activate')
def win():
window = Tk()
window.geometry ('500x500')
b1 = Button(text = 'Disabled', pady = '10', padx = '10', state = DISABLED)
b1.place(x=100, y=10)
b2 = Button(text = 'activate', pady = '10', padx = '10', command=com)
b2.place(x=10, y=10)
window.mainloop
win()
答案 0 :(得分:1)
您必须使用<buttonname>.config(state=STATE)
命令,其中STATE为:
Source我冒昧地创建编辑代码来测试它,它确实有效,见下文!
from Tkinter import *
def com1():
print ('activate')
b1.config(state = ACTIVE)
b2.config(state = DISABLED) #toggles the buttons
def com2():
print('de-activate')
b1.config(state = DISABLED)
b2.config(state = ACTIVE) #toggles the buttons
def win():
global b1
global b2 #This is to allow passing b1 and b2 to com1 and com2, but may not be the most "efficient" way to do this...
window = Tk()
window.geometry ('500x500')
b1 = Button(text = 'Disabled', pady = '10', padx = '10', state = DISABLED, command = com2) #command com2 added here
b1.place(x = 100, y = 10)
b2 = Button(text = 'activate', pady = '10', padx = '10', command = com1) #com changed to com1
b2.place(x = 10, y = 10)
window.mainloop()
win()