如何在Tkinter中更新此文本框的文本?

2024-04-29 15:14:31 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在python中用tkinter制作了一个秒表,我有一个更新工作时间的循环,但是我有它,循环会清除文本框,然后用新的数字更新文本框。在

虽然它不起作用,但由于某些原因它没有清除它,它只是不断地向框中添加数字。在

下面是我使用过的代码,如果有人能帮我看看这个,我会非常感激的:)

import time
from tkinter import *
root = Tk()
root.title("StopWatch")

#textbox for the screen
screen = Text(root, height = 1, width = 20, bd=10)
screen.grid(row=0, column=0)

#Active variable
global stopwatch_active
stopwatch_active = False
stop_time = 0
stop_minutes = 0


#command for starting the stopwatch
def start_com():
    stop_btn.config(state=NORMAL)
    stopwatch_active = True
    start_btn.config(state=DISABLED)
    global stop_time
    stop_time += 1
    screen.insert(END, stop_time)
    root.after(1000, start_com)




#button for starting the stopwatch
start_btn = Button(root, text = "Start", width = 10, bd = 5, command = start_com)
start_btn.grid(row=1, column=0, sticky=W)

#button for stopping the stopwatch
stop_btn = Button(root, text = "Stop", width = 10, bd = 5)
stop_btn.grid(row=1, column=0, sticky=E)
stop_btn.config(state=DISABLED)

Tags: thefortimecolumnrootwidthscreenstart
1条回答
网友
1楼 · 发布于 2024-04-29 15:14:31

添加:

screen.delete("1.0", END)

在此之前:

^{pr2}$

这将清除文本框中的所有文本。Effbot有更多信息,如果你感兴趣。这将产生类似于:

enter image description here

相关问题 更多 >