标签未更新

问题描述 投票:-1回答:1
def timer(self, delay, counter):
        while self.exitPro == False:
            while self.isOn:
                time.sleep(delay)
                counter += 1
                self.timed = counter
                print(self.timed)

Def Timer是一个在后台不断运行并正在更新的线程(这是有效的)

def main(self):
    timer = Thread(target=self.timer, args=(1, 0))
    timer.start()


    root = Tk()
    root.title("Jamie Stopwatch")
    v = IntVar()
    v.set(self.timed)
    stopwatch = ttk.Label(root, textvariable=v)
    stopwatch.grid(row=1, column=2)

现在我需要标签来更新计时器的值。

python multithreading python-3.x
1个回答
0
投票

使用after

def main(self):
    [...]
    self.stopwatch_value = IntVar()
    stopwatch = ttk.Label(root, textvariable=self.stopwatch_value)
    stopwatch.grid(row=1, column=2)
    root.after(self.update, 500)

def update(self):
    self.stopwatch_value.set(self.timed)
    root.after(self.update, 500)
© www.soinside.com 2019 - 2024. All rights reserved.