Tkinter: Difference between revisions

From David's Wiki
No edit summary
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 56: Line 60:
window = tk.Tk()
window = tk.Tk()
animation_loop()
animation_loop()
window.mainloop()
</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()
window.mainloop()
</syntaxhighlight>
</syntaxhighlight>
}}
}}

Revision as of 17:11, 1 February 2021

Tkinter is a Python API for the Tk GUI. It is built into the Python standard library and is cross platform.

Layout

tk.Frame

You can use tk.Frame as general containers.

Images

To display an image, create a tk.Canvas and then use canvsa.create_image.

Example
Notes
  • Make sure the ImageTk.PhotoImage does not get garbage collected.

Animation Loop

For interactive applications, you may want an animation loop called every few milliseconds.
To accomplish this, use .after.

Example

Keypress

To just detect key presses:

Example