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

编程入门 行业动态 更新时间:2024-10-24 03:25:11
本文介绍了如何在 Tkinter 中更新此文本框的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

所以我正在用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:54:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   文本   如何在   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!