【问题标题】:Tkinter image is blank [duplicate]Tkinter图像为空白[重复]
【发布时间】:2018-04-18 17:15:32
【问题描述】:

我有以下 python 代码:

from tkinter import *
from PIL import ImageTk, Image
import sys, os
height = 5
width = 8

# window = Tk()


class NSUI(Frame):
    def reload(self):
        os.execl(sys.executable, sys.executable, *sys.argv)
    def __init__(self, master=None):
        """
        Initialise process application
        """
        Frame.__init__(self, master)
        self.grid()
        self.master.title('PROGProject')
        # Configure columns
        for r in range(7):
            self.master.rowconfigure(r, weight=1)
        # Create columns
        for c in range(7):
            self.master.columnconfigure(c, weight=1)
            Button(master, text = "Actuele reistijden", bg = '#005ca0', fg = 'white', font = "Verdana 10", width = width, height = height, command = self.reload).grid(row = 5,column = 2,sticky = E+W)
            Button(master, text = 'Storingen', bg = '#005ca0', fg = 'white', font = "Verdana 10", width = width, height = height, command = self.reload).grid(row = 5,column = 4,sticky = E+W)

        Label(master, text = 'Welkom bij NS',font = "Verdana 50", bg = "#EFD10E", fg = "#005ca0").grid(row=0,column=1, columnspan=5)

        path = "test.jpg"
        img = ImageTk.PhotoImage(Image.open(path))
        panel = Label(root, image=img).grid(column=2,row=2)






root = Tk()

root.configure(background="#EFD10E")
root.tk.call('tk', 'scaling', 1.5)

app = NSUI(master=root)
app.mainloop()

它应该加载一个图像,图像在正确的目录中,我确定了这一点。但由于某种原因,它只加载一个白色方块。有谁知道这怎么可能?

我添加的相关代码:

path = "test.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img).grid(column=2,row=2)

提前致谢。

【问题讨论】:

标签: python tkinter pillow


【解决方案1】:

您必须将图像锚定到对象上。

path = "test.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img)
panel.photo = img
panel.grid(column=2,row=2)

【讨论】:

  • 成功了,我会在 5 分钟内接受这个答案:D
猜你喜欢
  • 2019-04-20
  • 1970-01-01
  • 2015-08-23
  • 2015-04-23
  • 1970-01-01
  • 2015-05-26
  • 2011-12-08
  • 1970-01-01
相关资源
最近更新 更多