Tkinter: Difference between revisions

From David's Wiki
Created page with "Tkinter is a Python API for the Tk GUI. It is built into the Python standard library and is cross platform. ==Images== To display an image: <syntaxhighlight lang="python"> im..."
 
 
(6 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==
To display an image:
To display an image, create a <code>tk.Canvas</code> and then use <code>canvsa.create_image</code>.
 
{{ hidden | Example |
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import tkinter as tk
import tkinter as tk
Line 19: Line 25:
photo_img = ImageTk.PhotoImage(image=image)
photo_img = ImageTk.PhotoImage(image=image)
canvas_image = canvas.create_image(0, 0, image=photo_img, anchor=tk.NW)
canvas_image = canvas.create_image(0, 0, image=photo_img, anchor=tk.NW)
window.mainloop()


# To update the image
# To update the image later on..
image_url2 = 'https://via.placeholder.com/256/FF00FF/'
image_url2 = 'https://via.placeholder.com/256/FF00FF/'
image2 = Image.open(requests.get(image_url, stream=True).raw)
image2 = Image.open(requests.get(image_url, stream=True).raw)
photo_img = ImageTk.PhotoImage(image=image2)
photo_img = ImageTk.PhotoImage(image=image2)
canvas.itemconfigure(canvas_image, image=photo_img)
canvas.itemconfigure(canvas_image, image=photo_img)
</syntaxhighlight>
}}
;Notes
* Make sure the <code>ImageTk.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 |
<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>
}}
==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>
}}


;Notes
;Notes
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.
* Use <code><KeyRelease></code> to detect key releases.

Latest revision as of 18:46, 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
import tkinter as tk
from PIL import Image, ImageTk
import requests

image_url = 'https://via.placeholder.com/256/0000FF/'
image = Image.open(requests.get(image_url, stream=True).raw)
window = tk.Tk()
main_frame = tk.Frame(window)
main_frame.pack()
canvas = tk.Canvas(main_frame,
                   width=image.size[0],
                   height=image.size[1])
canvas.pack()
photo_img = ImageTk.PhotoImage(image=image)
canvas_image = canvas.create_image(0, 0, image=photo_img, anchor=tk.NW)
window.mainloop()

# To update the image later on..
image_url2 = 'https://via.placeholder.com/256/FF00FF/'
image2 = Image.open(requests.get(image_url, stream=True).raw)
photo_img = ImageTk.PhotoImage(image=image2)
canvas.itemconfigure(canvas_image, image=photo_img)
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
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()

Keypress

To just detect key presses:

Example
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()
Notes
  • Use <KeyRelease> to detect key releases.