Answer the question
In order to leave comments, you need to log in
Why is the window not updating, but multiplying?
Hello everyone, tell me, after adding a cycle, instead of updating the main window, they multiply to infinity, and not the same windows with a ball, but adding a bunch of small and empty ones.
Without a loop, this program does not work, nothing is displayed on the screen.
With the use of mainloop(), 6 windows are displayed for some reason, and with the addition of a loop, an infinite number is also generated.
Where is the mistake?
How to run this code without mainloop i.e. nothing is written about it in the textbook (code example).
And why does window 6 pop out with it?
import tkinter
import random
import time
class Ball:
def __init__(self,canvas,color):
self.canvas = canvas
self.id = canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id, 245,100)
def draw(self):
pass
tk = tkinter
tk.Tk().title("game")
tk.Tk().resizable(0,0)
tk.Tk().wm_attributes("-topmost", 1)
canvas = tk.Canvas(tk.Tk(), width=500, height=400, bd=0,highlightthickness=0)
canvas.pack()
tk.Tk().update()
ball = Ball(canvas, "red")
while 1:
tk.Tk().update_idletasks()
tk.Tk().update()
time.sleep(0.01)
Answer the question
In order to leave comments, you need to log in
By calling Tk() every time you create a new instance of the class, the tk=tkinter line simply assigns the library to the tk variable. To create a single window, you must call Tk() only once and then modify only one instance of the class.
from tkinter import Tk, Canvas
import random
import time
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
def draw(self):
pass
tk = Tk()
tk.title("game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
ball = Ball(canvas, "red")
while True:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question