0

I was making python gui with tkinter I wanted to change button's image if I push button But it doesn't work.(I use config)

with some effort I found it work in main code but still it doesn't work in fucntion that is connected to button

Here are the codes

from tkinter import *
from PIL import Image, ImageTk

# using by function
def select_precision():
    overheal = image_resize((20, 20), "gui_project/item_simulation/overheal.png")
    runepage_00.config(image=overheal)

def image_resize(size, link):
    image = Image.open(link)
    image = image.resize(size, Image.ANTIALIAS)
    image = ImageTk.PhotoImage(image)
    return image

root = Tk()
root.title()

precision = PhotoImage(file="gui_project/item_simulation/precision.png")
rune00 = PhotoImage(file="gui_project/item_simulation/domination.png")

runepage_precision = Button(root, width=20, height=20,\
     image=precision, command=select_precision)
runepage_00 = Button(root, width=20, height=20, image=rune00)

runepage_precision.pack()
runepage_00.pack()

root.mainloop()

but if I change like this, the config works but I don't know why

from tkinter import *
from PIL import Image, ImageTk

def image_resize(size, link):
    image = Image.open(link)
    image = image.resize(size, Image.ANTIALIAS)
    image = ImageTk.PhotoImage(image)
    return image

root = Tk()
root.title()

precision = PhotoImage(file="gui_project/item_simulation/precision.png")
rune00 = PhotoImage(file="gui_project/item_simulation/domination.png")

runepage_precision = Button(root, width=20, height=20,\
     image=precision, command=select_precision)
runepage_00 = Button(root, width=20, height=20, image=rune00)

# using inside the code
overheal = image_resize((20, 20), "gui_project/item_simulation/overheal.png")
runepage_00.config(image=overheal)

runepage_precision.pack()
runepage_00.pack()

root.mainloop()

to make it short

first was

def select_precision():
    overheal = image_resize((20, 20), "gui_project/item_simulation/overheal.png")
    runepage_00.config(image=overheal)

runepage_precision = Button(root, width=20, height=20,\
     image=precision, command=select_precision)
runepage_00 = Button(root, width=20, height=20, image=rune00)

and second was

runepage_precision = Button(root, width=20, height=20,\
     image=precision, command=select_precision)
runepage_00 = Button(root, width=20, height=20, image=rune00)

overheal = image_resize((20, 20), "gui_project/item_simulation/overheal.png")
runepage_00.config(image=overheal)

0 Answers0