I am developing an application. The application needs to adjust itself every 1 second as new data comes in. but the application creates a new image downwards instead of editing the image.
from tkinter import *
from random import randint
import time
root = Tk()
lab = Label(root)
lab.pack()
def main():
y = randint(0,1000)
root.after(1000, main) # run itself again after 1000 ms
#random.randint(0, 250)
x= 250
print(y)
root.geometry("250x250")
canvas = Canvas()
canvas.create_line(0, y/2, x, y/2 , width=2 , fill="#ffffff") # Çizgi
canvas.create_rectangle(0, 0, 250, y/2-1,
outline="#0000CD", fill="#0000CD")# RENK
canvas.create_rectangle(0, 250, 250, y/2+1,
outline="#734a12", fill="#734a12") #RENK
#ara çizgiler
canvas.create_line(100, 145, 150, 145 , width=2 , fill="#ffffff")
canvas.create_line(110, 165, 140, 165, width=2 , fill="#ffffff")
canvas.create_line(100, 185, 150, 185 , width=2 , fill="#ffffff")
canvas.create_line(110, 205, 140, 205, width=2 , fill="#ffffff")
canvas.create_line(100, 225, 150, 225 , width=2 , fill="#ffffff")
canvas.create_line(110, 245, 140, 245, width=2 , fill="#ffffff")
canvas.create_line(100, 105, 150, 105 , width=2 , fill="#ffffff")
canvas.create_line(110, 85, 140, 85, width=2 , fill="#ffffff")
canvas.create_line(100, 65, 150, 65 , width=2 , fill="#ffffff")
canvas.create_line(110, 45, 140, 45, width=2 , fill="#ffffff")
canvas.create_line(100, 25, 150, 25 , width=2 , fill="#ffffff")
canvas.create_line(110, 5, 140, 5, width=2 , fill="#ffffff")
canvas.create_line(90, 125, 170, 125, fill="#ffff00" , width=3 ,dash=(4, 2))
canvas.pack(fill=BOTH, expand=1)
main()
root.mainloop()
I want to have only one of these but that’s how it works, thanks in advance for your help
>Solution :
You’re telling it to create something new every second. Move the code that updates the window to a separate function from the code that creates the widgets. Call main once, then call this other function every second.
