|
4 | 4 | #####################################################################
|
5 | 5 |
|
6 | 6 | from tkinter import *
|
| 7 | +from PIL import ImageTk, Image |
| 8 | + |
| 9 | +#pip install pillow |
7 | 10 |
|
8 | 11 | 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") |
12 | 15 |
|
13 | 16 | # 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") |
15 | 18 |
|
16 | 19 | # Create a canvas
|
17 | 20 | my_canvas = Canvas(root, width=800, height=500)
|
|
32 | 35 | button2_window = my_canvas.create_window(50, 10, anchor="nw", window=button2)
|
33 | 36 | button3_window = my_canvas.create_window(130, 10, anchor="nw", window=button3)
|
34 | 37 |
|
| 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) |
35 | 53 | root.mainloop()
|
36 |
| -Ñ |
| 54 | + |
37 | 55 |
|
0 commit comments