Skip to content

Commit ec93fa1

Browse files
feat Dynamically Resize Background Images complete
Dynamically Resize Background Images complete resolves: see also:
1 parent 5e964e6 commit ec93fa1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Python Tkinter Dynamically Resize Background Images/DynamicallyResizeBackgroundImages.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
#####################################################################
55

66
from tkinter import *
7+
from PIL import ImageTk, Image
8+
9+
#pip install pillow
710

811
root = Tk()
9-
root.title('Cambiar el tamaño de las imágenes de fondo de forma dinámica')
10-
root.iconbitmap('Cambiar el tamaño de las imágenes de fondo de forma dinámicas/icons/pikachu.ico')
11-
root.geometry("900x600")
12+
root.title('Python Tkinter Dynamically Resize Background Images')
13+
root.iconbitmap('Python Tkinter Dynamically Resize Background Images/icons/pikachu.ico')
14+
root.geometry("750x450")
1215

1316
# Define image
14-
bg = PhotoImage(file="Python Tkinter How To Use Images as Backgrounds/images/mario.png")
17+
bg = ImageTk.PhotoImage(file="Python Tkinter Dynamically Resize Background Images/images/mario.png")
1518

1619
# Create a canvas
1720
my_canvas = Canvas(root, width=800, height=500)
@@ -32,6 +35,21 @@
3235
button2_window = my_canvas.create_window(50, 10, anchor="nw", window=button2)
3336
button3_window = my_canvas.create_window(130, 10, anchor="nw", window=button3)
3437

38+
def resizer(e):
39+
global bg1, resize_bg, new_bg
40+
41+
bg1 = Image.open("Python Tkinter Dynamically Resize Background Images/images/mario.png")
42+
# Resize the image
43+
resize_bg = bg1.resize((e.width, e.height), Image.ANTIALIAS)
44+
# DEFINE OUR IMAGE AGAIN
45+
new_bg = ImageTk.PhotoImage(resize_bg)
46+
# add it back to the canvas
47+
my_canvas.create_image(0,0, image=new_bg, anchor="nw")
48+
# READ THE TEXT
49+
my_canvas.create_text(400, 250, text="Welcome!", font=("Helvetica", 50), fill="white")
50+
51+
52+
root.bind('<Configure>', resizer)
3553
root.mainloop()
36-
Ñ
54+
3755

Loading

0 commit comments

Comments
 (0)