How do I update this Text Box's text in Tkinter?

两盒软妹~` 提交于 2019-12-04 06:22:14

问题


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)

回答1:


Add:

screen.delete("1.0", END)

Before you do:

screen.insert(END, stop_time)

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



来源:https://stackoverflow.com/questions/27502418/how-do-i-update-this-text-boxs-text-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!