首页 > 解决方案 > 像 Angular 一样自动更新 tk.Label?

问题描述

如何根据另一个模块的变量自动更新一个。Label有没有“类似Angular”的模式?

以下内容不起作用,更多的是作为伪代码。

主文件

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

任何想法?

提前致谢!

标签: pythonpython-2.7tkinter

解决方案


textvariable如果您给它的变量是 tkinter 变量,它将自动更新。

import Tkinter as tk

points = tk.IntVar()

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

您最好在 learnpython.reddit.com 等面向初学者的论坛中提出此类问题


推荐阅读