Tkinter: Difference between revisions

No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Tkinter is a Python API for the Tk GUI. It is built into the Python standard library and is cross platform.
Tkinter is a Python API for the Tk GUI. It is built into the Python standard library and is cross platform.
==Layout==
===<code>tk.Frame</code>===
You can use <code>tk.Frame</code> as general containers.


==Images==
==Images==
Line 31: Line 35:
}}
}}
;Notes
;Notes
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.
* Make sure the <code>ImageTk.PhotoImage</code> does not get garbage collected.


==Animation Loop==
==Animation Loop==
Line 37: Line 41:
To accomplish this, use <code>.after</code>.
To accomplish this, use <code>.after</code>.


{{ hidden | Example |
{{ hidden | Example |
{{ hidden | Example |
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 60: Line 63:
</syntaxhighlight>
</syntaxhighlight>
}}
}}
==Keypress==
To just detect key presses:
{{ hidden | Example |
<syntaxhighlight lang="python">
import tkinter as tk
def key_pressed(event):
    print("Key pressed", event.char, event.keysym)
window = tk.Tk()
window.bind("<Key>", key_pressed)
window.mainloop()
</syntaxhighlight>
}}
;Notes
* Use <code><KeyRelease></code> to detect key releases.