Python 我的tkinter图像显示为黑屏
我正在为我的学校作业编写一个游戏。在这个游戏中,我想有一个静音按钮,所以我已经在标签框架顶部制作了一个按钮,并将其放置在标签中。我不知道它有什么问题,但图像没有显示出来。我尝试通过将其分配给临时变量来创建本地副本,但它仍然没有显示。这是我的密码:Python 我的tkinter图像显示为黑屏,python,image,tkinter,Python,Image,Tkinter,我正在为我的学校作业编写一个游戏。在这个游戏中,我想有一个静音按钮,所以我已经在标签框架顶部制作了一个按钮,并将其放置在标签中。我不知道它有什么问题,但图像没有显示出来。我尝试通过将其分配给临时变量来创建本地副本,但它仍然没有显示。这是我的密码: from tkinter import ttk from PIL import Image, ImageTk root = Tk() topFrame =Frame(root, width=500, height=50,) topFrame.gri
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()
尝试在禁用图像之前添加此项,它可能会工作
global mute\u image
和global mute\u icon
我实际上尝试运行此代码,它对我来说没有任何问题tho@CoolCloud没有任何问题:在global
范围内使用global…
是没有用的。无法重现您的行为,对我有用。验证你的图像!是的,这对我也有用,idk这里真正的问题是