-1

I'm learning Python and I decided to make a GUI. It's a simple GUI that'll just move a polygon on the screen. But it seems the thread keep getting stuck in the update method.

class main():

    render = Render(500, 500)

    while True:
        render.update()
        time.sleep(1)


class Render:

    def __init__(self, width, height):
        self.window = Tk()
        self.window.config(height=500, width=500)
        self.canvas = Canvas(self.window, width = width, height = height, 
                             background = "white")
        self.deltaX = 0


    def update(self):
        print(self.deltaX)
        self.canvas.create_polygon(150 + self.deltaX, 75, 225 + self.deltaX, 0, 
                                   300 + self.deltaX, 75, 225 + self.deltaX, 150, 
                                   outline = "black", fill = "")
        self.canvas.pack()
        self.window = mainloop()
        self.deltaX += 10
        return None

Sorry if this is dumb, but I'm very new to Python.

martineau
  • 119,623
  • 25
  • 170
  • 301
CSHelp762
  • 7
  • 1
  • Moving a polygon on the screen is not as simple as you seem to think. I suggest you try something easier until you've learned Python better. Besides that, using `tkinter` to make a GUI can also be difficult to learn, even if you know Python well. – martineau Aug 02 '22 at 01:50
  • Does this answer your question? [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – gre_gor Aug 08 '22 at 09:18

3 Answers3

2

You shouldn't call mainloop like that, probably you should only call it once. See Tkinter understanding mainloop

Osinaga
  • 39
  • 3
0

There's a couple ways to write this out. No matter how you do it you only need to call/draw the object once and then update it through your loop

It can be as simple as creating a property in the constructor and then updating via a method

from tkinter import *
import time

class Render:

    def __init__(self, width, height):
        self.window = Tk()
        self.window.config(height=500, width=500)
        self.canvas = Canvas(self.window, width = width, height = height, 
                             background = "white")
        self.deltaX = 2
        self.poly = self.canvas.create_polygon(150 + self.deltaX, 75, 225 + self.deltaX, 0, 
                                   300 + self.deltaX, 75, 225 + self.deltaX, 150, 
                                   outline = "black", fill = "")
        self.canvas.pack()
    def update(self):
        self.canvas.move(self.poly, self.deltaX, 0)
        self.window.update()    


class main():

    render = Render(500, 500)

    while True:
        render.update()
        time.sleep(1)
Justin
  • 2,873
  • 3
  • 9
  • 16
0

Moving one or more objects in tk.Canvas is fairly easy.

Here is an interactive demo that enables one or more objects to be positioned with simple left mouse button click.

It uses tags to name the object|s and event_add to bind multiple keyboard|mouse activity to function pickNplace

master = tk.Tk()

canvas = tk.Canvas(master, width = 600, height = 400)
canvas.grid(sticky = tk.NSEW)
xx = yy = moveit = 0

def pickNplace(event):
    global moveit, xx, yy
    x, y = event.x, event.y
    if event.num == 1:
        moveit = 1 - moveit
    if moveit:
        canvas.move("poly", x - xx, y - yy)
    xx, yy = x, y

canvas.event_add("<<MOVER>>", "<Motion>", "<ButtonRelease-1>")
canvas.bind("<<MOVER>>", pickNplace)

canvas.create_polygon(
    0, 0, 100, 0, 100, 100, 0, 100,
    fill = "blue", outline = "blue", tags = "poly")

master.mainloop()
Derek
  • 1,916
  • 2
  • 5
  • 15