我的 tkinter 图像显示为黑屏



我正在尝试为我的学校作业编写游戏。在这个游戏中,我想有一个静音按钮,所以我在标签框架的顶部做了一个按钮,并将其放在标签中。我不知道它有什么问题,但图像没有显示。我尝试通过将本地副本分配给临时变量来创建本地副本,但它仍然没有显示。这是我的代码:

from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
topFrame =Frame(root, width=500, height=50,)
topFrame.grid(row=0, column= 0)
btnframe = LabelFrame(topFrame, width = 20, height = 20)
btnframe.place(x = 450, y= 5 )
mute_image = Image.open("pygame/mute.png")
mute_image = mute_image.resize((50,50), Image.ANTIALIAS)
mute_icon = ImageTk.PhotoImage(mute_image)
mute_button = Button( btnframe, width = 50, height = 50, command = Mute, image = mute_icon)
mute_button.image = mute_icon
mute_button.pack()
root.mainloop()

请放轻松,这是我第一次编写游戏:((提前感谢:((

您是否收到任何错误消息。我稍微修改了您的代码,它抱怨按钮回调函数未定义。在我补充说一切似乎都有效之后。

from tkinter import *
from PIL import Image, ImageTk
root = Tk()
topFrame =Frame(root, width=500, height=50,)
topFrame.grid(row=0, column= 0)
btnframe = LabelFrame(topFrame, width = 20, height = 20)
btnframe.place(x = 450, y= 5 )
def Mute():
pass
mute_image = Image.open("images/beer.png")  
mute_image = mute_image.resize((50,50), Image.ANTIALIAS)
mute_icon = ImageTk.PhotoImage(mute_image)
mute_button = Button(btnframe, width=50, height=50,
command=Mute, image=mute_icon)
mute_button.image = mute_icon
mute_button.pack()
root.mainloop()

最新更新