如何在 Tkinter 中更新此文本框的文本? [英] How do I update this Text Box's text in Tkinter?

查看:95
本文介绍了如何在 Tkinter 中更新此文本框的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am making a stopwatch in python with tkinter, I have the loop for updating the time working, but I have it so the loop clears the text box, and then updates the text box with the new number.

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

Although it doesnt work, for some reason it just doesnt clear it, it just keeps adding numbers to the box.

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

Here is the code that I have used, if someone would be able to have a look at this for me I would appriciate it a lot :)

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)

推荐答案

添加:

screen.delete("1.0", END)

在此之前:

screen.insert(END, stop_time)

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

This will clear all the text from the text box. Effbot has more information if your interested. This will produce something similar to:

这篇关于如何在 Tkinter 中更新此文本框的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆