像在Angular中一样自动更新tk.Label吗?

时间:2018-08-05 16:18:20

标签: python python-2.7 tkinter

如何根据另一个模块的变量自动更新 Label。有任何“类似角”的图案吗?

以下内容无效,更多是伪代码。

main.py

import myStatusStoreModule

# The label should update when points changes
Label(self, textvariable=myStatusStoreModule.points) 

myStatusStoreModule.py

points = 0

def addPoints(p):
    global points
    points += p

# here could be a event listener that modifies points as well

有什么主意吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

textvariable将自动更新如果您提供的变量是tkinter变量。

import Tkinter as tk

points = tk.IntVar()

def addPoints(p):
    global points
    points.set(points.get() + p)

您最好在一个面向初学者的论坛(例如:Learnpython.reddit.com)中提出这样的问题