Question

This is what I am trying to do. Very simple code. Draw line from one point to another gradually. Here's my code:

from Tkinter import *
import time

mGui = Tk()
mGui.title('GUI Example')
mGui.geometry('1024x576+10+30')

#Canvas
canvas_1 = Canvas(mGui,height=500,width=500,bg='grey')

for i in range(1, 500):
    time.sleep(0.5)
    canvas_1.create_line(0,0,i,i)
    canvas_1.update()

canvas_1.pack()

mGui.mainloop()

My window just stays blank white 0.5*500 seconds (250 seconds) and then canvas shows up with the line drawn. What am I doing wrong?

Was it helpful?

Solution

You need to call the pack method on the canvas before you start the loop:

from Tkinter import *
import time

mGui = Tk()
mGui.title('GUI Example')
mGui.geometry('1024x576+10+30')

#Canvas
canvas_1 = Canvas(mGui,height=500,width=500,bg='grey')

#################
canvas_1.pack()
#################

for i in range(1, 500):
    time.sleep(0.5)
    canvas_1.create_line(0,0,i,i)
    canvas_1.update()

mGui.mainloop()

Otherwise, the canvas will not be placed on the window until after the loop exits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top