我的tkinter图像显示为黑屏

2024-04-28 19:53:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()

请对我放松点,这是我第一次编写游戏:)提前谢谢:)


Tags: fromimageimport游戏buttonroot标签width
1条回答
网友
1楼 · 发布于 2024-04-28 19:53:29

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

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()

相关问题 更多 >