我在 Tkinter 中添加为按钮的图像不可见,而文本版本是可见的

The image I am adding as a button in Tkinter is not being visible, Where as the text version is visible

在我的项目中,为了自定义按钮,我向其添加了图像...所有其他按钮都可以很好地处理这些图像,但是当我尝试在另一个根目录 Toplevel 中添加图像时,按钮上的图像不可见,甚至无法点击,代码如下:

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)

这是按钮代码:

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)

这是输出: img

在设置菜单中,图像不可见且不可点击

您可以使用另一个名为 PIL 的库。

例如:

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)

我设置 sett.applyimg 的原因是,如果你不这样做,它将被垃圾收集,因此不会显示。

希望对您有所帮助!