pythontkinter

Image size can't be changing (python,tkinter)


In my program , there is a button that showing "Click Me". When the user click the button, it will change the text element to display a png file (icon1.png).

The program and function can be execute and run , but the the image too small already, so what i done is resize the image size using Pillow (PIL). But it not work and whatever i change the value (size) , the image still not effected.

This is the output:

Button before clicked Button after clicked

Here is my currently code:

import tkinter as tk
from PIL import Image, ImageTk

def button_clicked():
    button.config(image=photo, text="")  

root = tk.Tk()

original_image = Image.open("icon1.png") 
resized_image = original_image.resize((100, 100))
photo = ImageTk.PhotoImage(resized_image)

button = tk.Button(root, 
                   text="Click Me", 
                   command=button_clicked,
                   activebackground="blue", 
                   activeforeground="white",
                   anchor="center",
                   bd=3,
                   bg="lightgray",
                   cursor="hand2",
                   disabledforeground="gray",
                   fg="black",
                   font=("Arial", 12),
                   height=2,
                   highlightbackground="black",
                   highlightcolor="green",
                   highlightthickness=2,
                   justify="center",
                   overrelief="raised",
                   padx=10,
                   pady=5,
                   width=15,
                   wraplength=100)

button.pack(padx=20, pady=20)

root.mainloop()

Here is my information of the image that i want to display(icon1.png): icon1.png

May i know how to fix the problem and any other way that can resize the image size ? Thank !


Solution

  • in your code you defined the width = 15 and the height = 2 of the button in button defined and you can change it in the function as this

    def button_clicked():
        button.config(image=photo, text="", height = 100, width = 100)