python - Tkinter understanding after() -


first of all, take @ previous thread here: tkinter understanding mainloop

after following advice there, in gui programming, infinite loops have avoided @ costs, in order keep widgets responsive user input.

instead of using:

while 1:     ball.draw()     root.update()     time.sleep(0.01) 

i managed using self.canvas.after(1, self.draw) inside draw() function.

so code looks this:

# testing skills in game programming  tkinter import *  root = tk() root.title("python game testing") root.resizable(0, 0) root.wm_attributes("-topmost", 1)  canvas = canvas(root, width=500, height=400, bd=0, highlightthickness=0) canvas.pack() root.update()  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)          self.canvas_height = canvas.winfo_height()         self.x = 0         self.y = -1      def draw(self):         self.canvas.move(self.id, self.x, self.y)          pos = self.canvas.coords(self.id)         if pos[1] <= 0:             self.y = 1         if pos[3] >= self.canvas_height:             self.y = -1          self.canvas.after(2, self.draw)   ball = ball(canvas, "red") ball.draw()  root.mainloop() 

however, time inside self.canvas.after() not work properly... if set 1 it's extremely fast! if it's set 10, 5 or 2, it's slow! didn't have problem while using above while loop in code since time.sleep() worked should!


edit:

i can report time inside after function of tkinter not work in windows 8.1 tablet, , in windows 8.1 laptop, while in same laptop when running ubuntu through virtual machine work should.

time in time.sleep in seconds whereas in after() in milliseconds. time.sleep(0.01) same self.canvas.after(10, self.draw). if after(2, func) slow after(1, func) fast try sleep(0.0005) after(1, func) give delay of 1.5 milliseconds barely noticeable. either way, fiddle timing until pause right.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -