每当全局变量a
更改其值时,我怎样才能调用函数change_label
?用change_variable
我想模拟变量的实际变化(点击按钮时的变量变化)。如何在全局变量更改值时调用函数?
from tkinter import *
a = 3
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.button = Button(self.master, text='Change Variable', command=self.change_variable)
self.button.grid(row=0)
self.label = Label(self.master, text='Test')
self.label.grid(row=1)
def change_label(self):
self.label.config(bg='Red', fg='Yellow')
def change_variable(self):
global a
a = 1
def main():
root = Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()
如果它不是一个问题,你可以使用一个轻量级线程定期监视该值。 –
好吧,你为什么不在所有改变'a'的地方调用'change_label'? – MSeifert
此外,请检查:[如何触发函数的值更改?](https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change) –