17 lines
766 B
Python
17 lines
766 B
Python
import tkinter as tk
|
|
from PIL import Image, ImageTk
|
|
|
|
class ResizingBG(tk.Canvas):
|
|
"""Ein Canvas, das ein PIL-Bild automatisch an seine eigene Größe anpasst (Strecken)."""
|
|
def __init__(self, master, raw_image, **kwargs):
|
|
super().__init__(master, highlightthickness=0, bd=0, **kwargs)
|
|
self.raw_image = raw_image
|
|
self.bg_img = None
|
|
self.bind("<Configure>", self.on_resize)
|
|
|
|
def on_resize(self, event):
|
|
if self.raw_image and event.width > 10 and event.height > 10:
|
|
resized = self.raw_image.resize((event.width, event.height), Image.Resampling.LANCZOS)
|
|
self.bg_img = ImageTk.PhotoImage(resized)
|
|
self.delete("all")
|
|
self.create_image(0, 0, image=self.bg_img, anchor="nw") |