Tkinter: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:


==Images==
==Images==
To display an image
To display an image, create a <code>tk.Canvas</code> and then use <code>canvsa.create_image</code>.
 
{{ hidden | Example |
{{ hidden | Example |
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 31: Line 32:
;Notes
;Notes
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.
==Animation Loop==
For interactive applications, you may want an animation loop called every few milliseconds.<br>
To accomplish this, use <code>.after</code>.
{{ hidden | Example |
{{ hidden | Example |
<syntaxhighlight lang="python">
import tkinter as tk
import time
last_time = time.time()
def animation_loop():
    global last_time
    now = time.time()
    delta_time = now - last_time
    print("Time elapsed", delta_time)
    last_time = now
    window.after(1, animation_loop)
window = tk.Tk()
animation_loop()
window.mainloop()
</syntaxhighlight>
}}