Python ImageTk.PhotoImage不';不会在OSX上显示,但会在Windows上显示
我的这个脚本在Windows中运行时会显示logo.png图像,但在Mac OS X终端中运行时不会显示图像(即使它生成了足够大的空间) 我曾尝试创建一个单独的变量,如此问题的解决方案所述,但无法加载图像 非常感谢你的帮助Python ImageTk.PhotoImage不';不会在OSX上显示,但会在Windows上显示,python,macos,python-2.7,tkinter,python-imaging-library,Python,Macos,Python 2.7,Tkinter,Python Imaging Library,我的这个脚本在Windows中运行时会显示logo.png图像,但在Mac OS X终端中运行时不会显示图像(即使它生成了足够大的空间) 我曾尝试创建一个单独的变量,如此问题的解决方案所述,但无法加载图像 非常感谢你的帮助 # -*- coding: utf-8 -*- import Tkinter as tk import ttk import tkMessageBox from PIL import ImageTk, Image class TestPage(tk.Frame):
# -*- coding: utf-8 -*-
import Tkinter as tk
import ttk
import tkMessageBox
from PIL import ImageTk, Image
class TestPage(tk.Frame):
def __init__(self, master, text, height, width, *args, **kwargs):
tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
self.height = height
self.width = width
#Test Frame
self.testFrame = tk.Frame(self)
self.logo = ImageTk.PhotoImage(Image.open('logo.png'))
global logo
logo = self.logo
self.logolabel=tk.Label(self.testFrame, image=self.logo)
self.logolabel.pack()
self.testFrame.pack(side=tk.LEFT, fill='both', expand = 'yes')
self.update()
self.onlift()
def onlift(self):
root.geometry('{}x{}'.format(self.width, self.height))
self.lift()
root.update()
class App(tk.Frame):
def __init__(self, *args, **kwargs):
global p1
tk.Frame.__init__(self, *args, **kwargs)
p1 = TestPage(self, 'blank', height=root.winfo_screenheight(), width=root.winfo_screenwidth())
p1.place(x=0, y=0, relwidth=1, relheight=1)
root.update()
global p1
root = tk.Tk()
root.title('LTEST01 V2.0 GUI')
app = App(root)
root.update()
root.mainloop()
我也有同样的问题。转换为RGB解决了这个问题 因此,请按照以下方式编写代码:
self.logo = ImageTk.PhotoImage(Image.open('logo.png').convert('RGB'))
基本上,带有Alpha通道的透明图像似乎不起作用 似乎“透明掩码”、MACOS和pyinstaller的组合不起作用。RGBA(4x8位像素,带透明遮罩的真彩色)也不起作用。遗憾的是,只有转换为RGB才能解决问题。如果您删除了所有与此问题无关的代码,这将非常有帮助。例如,您的页面实际上只需要包含图像的标签。看到了吗?可能是图像文件路径问题吗?
Image.open()
的参数不一定只是一个文件名,它是一个文件路径,在两种平台上可能会有所不同。此外,正如@Bryan所建议的,请回答您的问题,以便其中的代码是重现问题的最低要求。请参阅。@BryanOakley代码现在是minimized@martineau奇怪的是,将文件转换成jpeg格式居然奏效了!所以我认为你是对的,这是一个枕头问题。我会报告的。