window resize speeding up root.after()?

Post here if you need help with creating a Graphical User Interface in Python.

window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 10:12 am

The following code draws a simple clock face using root.after() It works fine but resizing the window by dragging a corner causes the clock to speed up (while the resize drag is taking place). I would like it to be unaffected by a resize by dragging, is there anything I can do?

Code: Select all
from Tkinter import *
from math import cos,pi,sin,exp,sqrt

root=Tk()
root.minsize(100,100)

w = Canvas(root)
w.pack(fill=BOTH,expand=TRUE)

#initialise the clock
angle=0.0
increment=0.01
w.create_oval(0, 0, 80, 80, outline="red", width=2)
phase=w.create_line(40,40,40+40*int(sin(angle)),40+40*int(cos(angle)))

pixelX=root.winfo_width()
pixelY=root.winfo_height()
 
def update_clock():
    global angle,phase
    angle+=increment
    w.coords(phase,40,40,40+int(40*sin(angle)),40+int(40*cos(angle)))
    root.after(1, update_clock)

update_clock()

root.mainloop()
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am

Re: window resize speeding up root.after()?

Postby metulburr » Fri Aug 30, 2013 10:29 am

i am not sure what you mean, as for me, it stays the same when resizing the window.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1362
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 10:37 am

Really? For me while I'm dragging the window to resize it runs about twice as fast :( Also the same when I just move the window without resizing. Did you run in 2.7 or a later version? I'm using 2.7
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am

Re: window resize speeding up root.after()?

Postby metulburr » Fri Aug 30, 2013 10:50 am

no i tried 3.x, but i just ran it in 2.x and it also seems to be consistant.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1362
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 10:59 am

I just installed 3.3 and still the same for me - however I realise I'm daft to rely on after for my timing, I could just use the system clock and save myself the problem that way. Thanks for taking the time, it was really useful if just to see that I wasn't doing something stupid code wise.
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am

Re: window resize speeding up root.after()?

Postby metulburr » Fri Aug 30, 2013 11:01 am

i tested it on linux. Maybe your on windows? Not sure.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1362
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 11:09 am

Ahh, that could make all the difference, yes I am on windows 7 for my sins.
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am

Re: window resize speeding up root.after()?

Postby Mekire » Fri Aug 30, 2013 12:05 pm

Interesting. Windows 7 here too and it does indeed run faster when moving the window.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 12:59 pm

For anyone who's interested, a better code!
Code: Select all
from Tkinter import *
from math import cos,pi,sin,exp,sqrt
from time import clock

root=Tk()
root.minsize(100,100)

w = Canvas(root)
w.pack(fill=BOTH,expand=TRUE)

#initialise the clock
w.create_oval(0, 0, 80, 80, outline="red", width=2)
phase=w.create_line(0,0,0,0)
 
def update_clock():
    t_secs=clock()
    omega=1.0
    angle=omega*t_secs
    w.coords(phase,40,40,40+int(40*sin(angle)),40+int(40*cos(angle)))
    root.after(3, update_clock)

update_clock()

root.mainloop()
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am

Re: window resize speeding up root.after()?

Postby Yoriz » Fri Aug 30, 2013 1:02 pm

I dont think the call to after is guarantied to be exactly after the amount of time you set.
I guess the gui's event loop speed is changed when resizing, possibly theres less for it to do when its resizing the frame so it calls your method faster.
If you use the clock to measure the actual time passed you won't get the speed up.
Code: Select all
from Tkinter import *
from math import cos,pi,sin,exp,sqrt
import time

root=Tk()
root.minsize(100,100)

w = Canvas(root)
w.pack(fill=BOTH,expand=TRUE)

#initialise the clock
angle=0.0
increment=0.01
w.create_oval(0, 0, 80, 80, outline="red", width=2)
phase=w.create_line(40,40,40+40*int(sin(angle)),40+40*int(cos(angle)))

pixelX=root.winfo_width()
pixelY=root.winfo_height()
 
def update_clock():
    global angle,phase
#     angle+=increment
    angle = time.clock()
    w.coords(phase,40,40,40+int(40*sin(angle)),40+int(40*cos(angle)))
    root.after(1, update_clock)

time.clock()
update_clock()

root.mainloop()
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 758
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: window resize speeding up root.after()?

Postby listbox » Fri Aug 30, 2013 1:14 pm

Thanks Yoriz, things often seem just that little bit less perfect on Windows, you could be spot on there with that analysis.
listbox
 
Posts: 6
Joined: Fri Aug 30, 2013 10:04 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests