ImageTk.PhotoImage不会显示在OSX上,但会显示在Windows上

时间:2017-02-07 21:00:29

标签: python macos python-2.7 tkinter python-imaging-library

当我在Mac OS X终端上运行脚本时,我的这个脚本在Windows中运行时会显示logo.png图像,但不会显示图像(即使它为它生成足够大的空间)。

我试图创建一个单独的变量,如此问题的解决方案所描述,但无法加载图像。

非常感谢您的帮助!

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

2 个答案:

答案 0 :(得分:1)

我有同样的问题。转换为RGB解决了该问题。

因此使您的代码如下:

self.logo = ImageTk.PhotoImage(Image.open('logo.png').convert('RGB'))

基本上,带有Alpha通道的透明图像似乎不起作用。

答案 1 :(得分:0)

似乎“透明掩码”,MACOS和pyinstaller的组合不起作用。 RGBA(4x8位像素,带透明蒙版的真彩色)不能正常工作。可悲的是,仅转换为RGB即可解决问题。

相关问题