Moving a window on the screen
Terry Reedy
tjreedy at udel.edu
Sat Nov 8 13:40:53 EST 2014
On 11/8/2014 11:35 AM, Akira Li wrote:
> "ast" <nomail at invalid.com> writes:
>
>> Ok, thx, it works now with:
>>
>> import tkinter
>> fen = tkinter.Tk()
>>
>> x=0
>>
>> def moveW():
>> global x
>> fen.geometry("200x200+%d+10" % x)
>> x = x + 10
>> if (x < 1200):
>> fen.after(50, moveW)
>>
>> moveW()
>
> In general, to avoid the start time "drift" [1],
which is hardly noticeable
> you could lock the
> execution with a timer e.g., to move the window from left to right
> *delta_x* pixels at a time every *period* ms [2]:
On my Win7 machine, your complicated code is much worse as it causes the
window to jump about every half second
> #!/usr/bin/env python3
> from time import monotonic
> from tkinter import Tk
>
> def timer():
> return int(monotonic() * 1000) # milliseconds
>
> def call_repeatedly(period, function, *args):
> root.after(period - timer() % period, call_repeatedly, period,
The '- timer() % period' 'correction' is wrong when not 0 as it causes
jumps.
> function, *args) # schedule the next call
> function(*args)
>
> def move(delta_x, max_x, width=200, x=[0]):
> root.geometry("%dx50+%d+100" % (width, x[0]))
> x[0] += delta_x # poor man's object
> if x[0] > (max_x - width):
> root.destroy() # exit
>
> root = Tk()
> period = 20 # call every *period* milliseconds
> delta_x = 2 # how many pixels to move at a time
> root.after(period - period % timer(), call_repeatedly, period,
'period % timer()' is nonsensical as timer() is arbitrary. It will
typically be 0 anyway. 'after(0, ...)' works fine.
> move, delta_x, root.winfo_screenwidth())
> root.mainloop()
>
>
> [1]: http://stackoverflow.com/questions/8600161/executing-periodic-actions-in-python#comment26637231_8600301
> [2]: http://stackoverflow.com/questions/24174924/how-to-run-a-function-periodically-in-python
>
>
> Akira
>
--
Terry Jan Reedy
More information about the Python-list
mailing list