Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Last active November 10, 2018 07:12
Show Gist options
  • Select an option

  • Save gottadiveintopython/a371371574730d8a645235f44df3c8af to your computer and use it in GitHub Desktop.

Select an option

Save gottadiveintopython/a371371574730d8a645235f44df3c8af to your computer and use it in GitHub Desktop.
tkinter版のyieldsleep
from functools import wraps
def yieldsleep(widget):
def func(create_gen):
@wraps(create_gen)
def func2(*args, **kwargs):
gen = create_gen(*args, **kwargs)
def sleep(milliseconds):
widget.after(milliseconds, resume_gen)
def resume_gen():
try:
sleep(next(gen))
except StopIteration:
pass
resume_gen()
return gen
return func2
return func
from tkinter import *
root = Tk()
label = Label(root, font=('', 80))
label.pack(fill='both')
@yieldsleep(label)
def animate_label(label):
while True:
yield 0
label['text'] = 'Do'
yield 500
label['text'] = 'You'
yield 500
label['text'] = 'Like'
yield 500
label['text'] = 'Tkinter?'
yield 1500
gen = animate_label(label)
button = Button(root, text='stop animation', command=gen.close)
button.pack(fill='both')
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment