0

In My project, to customize the buttons, I added image to it...All other buttons are working well with the images, but when i am trying to add image in another screen that is Toplevel of root, the images on the buttons are not visible and not even clickable, here is the code:

def settings():
global sett
sett = Toplevel(Tops)
sett.title('Settings')
sett.geometry('600x550')

Label(sett, text='').grid(row=0, column=0)
Label(sett, text='')
global biriyani_entry1
global Chicken65_entry1
global coke_entry1
global vf_rice1
global samosa_entry1
global tea_entry1
global Noodles_entry1
global s1
global s2
global s3
global s4
global s5
global s6
global s7

s1 =Label(sett, text='Biriyani: ', font=('calibri', 20))
s1.grid(row=1, column=0)
biriyani_entry1 = Entry(sett, font=('calibri', 20))
biriyani_entry1.grid(row=1, column=1)
biriyani_entry1.insert(0, 200)

Label(sett, text='').grid(row=2, column=0)
Label(sett, text='').grid(row=2, column=1)

s2 = Label(sett, text='Chicken 65: ', font=('calibri', 20))
s2.grid(row=3, column=0)
Chicken65_entry1 = Entry(sett, font=('calibri', 20))
Chicken65_entry1.grid(row=3, column=1)
Chicken65_entry1.insert(0, 180)
Label(sett, text='').grid(row=4, column=0)
Label(sett, text='').grid(row=4, column=1)

s3 = Label(sett, text='Coke ', font=('calibri', 20))
s3.grid(row=5, column=0)
coke_entry1 = Entry(sett, font=('calibri', 20))
coke_entry1.grid(row=5, column=1)
coke_entry1.insert(0, 20)
Label(sett, text='').grid(row=6, column=0)
Label(sett, text='').grid(row=6, column=1)

s4 = Label(sett, text='Veg Fried Rice:  ', font=('calibri', 20))
s4.grid(row=7, column=0)
vf_rice1 = Entry(sett, font=('calibri', 20))
vf_rice1.grid(row=7, column=1)
vf_rice1.insert(0, 120)
Label(sett, text='').grid(row=8, column=0)
Label(sett, text='').grid(row=8, column=1)

s5 = Label(sett, text='Samosa ', font=('calibri', 20))
s5.grid(row=9, column=0)
samosa_entry1 = Entry(sett, font=('calibri', 20))
samosa_entry1.grid(row=9, column=1)
samosa_entry1.insert(0, 15)
Label(sett, text='').grid(row=10, column=0)
Label(sett, text='').grid(row=10, column=1)

s6 = Label(sett, text='Tea ', font=('calibri', 20))
s6.grid(row=11, column=0)
tea_entry1 = Entry(sett, font=('calibri', 20))
tea_entry1.grid(row=11, column=1)
tea_entry1.insert(0, 10)
Label(sett, text='').grid(row=12, column=0)
Label(sett, text='').grid(row=12, column=1)

s7 = Label(sett, text='Noodles ', font=('calibri', 20))
s7.grid(row=13, column=0)
Noodles_entry1 = Entry(sett, font=('calibri', 20))
Noodles_entry1.grid(row=13, column=1)
Noodles_entry1.insert(0, 150)
Label(sett, text='').grid(row=14, column=0)
Label(sett, text='').grid(row=14, column=1)
Label(sett, text='').grid(row=15, column=0)
Label(sett, text='').grid(row=15, column=1)


'''fr = Frame(sett, width = 599, height = 101)
fr.configure(bg='black')
fr.place(x = 1, y =450)'''
applyimg = PhotoImage(file='apply_bt.png')
apply_button = Button(sett,image = applyimg, font=('calibri', 17),command=setting_change, borderwidth=10)
apply_button.grid(row=16, column=1)

database_img = PhotoImage(file = 'dta.png')
db_button = Button(sett,image = database_img , command=dbshow,font=('calibri', 15))
db_button.grid(row=16, column=0)

This is the button code:

applyimg = PhotoImage(file='apply_bt.png')
apply_button = Button(sett,image = applyimg, font=('calibri', 17),command=setting_change, borderwidth=10)
apply_button.grid(row=16, column=1)

database_img = PhotoImage(file = 'dta.png')
db_button = Button(sett,image = database_img , command=dbshow,font=('calibri', 15))
db_button.grid(row=16, column=0)

This is the output: img

In the settings menu, the images are not visible and not clickable

vsmeo
  • 5
  • 2
  • 3
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – acw1668 May 31 '21 at 05:29
  • You can use PIL lib –  May 31 '21 at 06:10

1 Answers1

0

What you can do is use another library called PIL.

For example:

from PIL import Image, ImageTk

load_applyimg = Image.open("apply_bt.png")
sett.applyimg = ImageTk.PhotoImage(load_applyimg)

apply_button = Button(sett,image = sett.applyimg, font=('calibri', 17),command=setting_change, borderwidth=10)
apply_button.grid(row=16, column=1)

The reason i set sett.applyimg is that if you don't it will be garbage collected, and thus not show up.

Hope this helped!

Itsjul1an
  • 301
  • 1
  • 2
  • 12