Python Tkinter: Screen isn’t updating with after/mainloop

I have been working on 3D graphics using Python Tkinter and everything work except for the screen not actually updating. I’ve tried a few things and nothing has worked (root.update inside a while loop, root.after inside a definition and then a mainloop, others i can’t remember).

def main():
    global c
    global yDif
    reset()
    rotateX(xDif)
    rotateY(yDif)
    to2D()
    for i, val in enumerate(triangles1):
        c = (colors[int(i/3)])
        sort()
    yDif += 10 #yDif is changing but the graphics don't update
    print("done") #This is working it is repeatedly printing to console after 1 sec
    root.after(0, root.update) #Idk if I need this I was just trying random stuff
    root.after(1000, main)

main()
root.mainloop()

The main() function runs on a loop but the screen doesn’t reflect that. If my goal is to have it rotate on its own (call the main function with no input) and then update the screen accordingly every time how should I go about this?

Edit: Important info I left out: There are create_line calls in a different part of the program and just to be clear everything functions like normal for the first iteration (meaning I do end up with visuals) but after that they never update again to reflect changes in the drawn lines that I know are changing.

Here is what is creating the lines. It is a part of the main function

def centeredLine(x1,y1,x2,y2):
    win.create_line(x1 + w/2, (0-y1) + h/2, x2 + w/2, (0-y2) + h/2, fill=c)

  • It’s pointless to call update with after since everyting in the “after” queue only execute after the screen has been updated.

    – 

Nowhere in your code do you do anything that affects the screen. Once you do the calculations, you need to explicitly set a label or print to a text widget or draw an object, or something like that. The tkinter widgets know nothing about your variable yDiff.

If you want to update lines on a canvas based on calculation, you’ll have to explicitly call the itemconfigure method of the canvas. You will need to pass an id or tag that represents the line, and the new coordinates.

Leave a Comment