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)
matsjoyce
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