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

Jam*_*ner 1 python tkinter stopwatch python-3.x

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

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

这是我使用的代码,如果有人能够帮我看看这个,我会非常感谢它:)

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)
Run Code Online (Sandbox Code Playgroud)

mat*_*yce 5

添加:

screen.delete("1.0", END)
Run Code Online (Sandbox Code Playgroud)

在你做之前:

screen.insert(END, stop_time)
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明