【问题标题】:Python tkinter PhotoImage not working correctlyPython tkinter PhotoImage 无法正常工作
【发布时间】:2017-10-07 20:03:27
【问题描述】:

我正在尝试使用 tkinter,但此代码不起作用,我想知道是否有人知道为什么谢谢。

from tkinter import *
window = Tk()
window.title("tkinter stuff")
photo1 = PhotoImage("file=hs.gif")
Label(window, image=photo1).grid(row=0,column=0,sticky=W)
window.mainloop()

为了澄清,出现了一个名为“tkinter stuff”的窗口,但图像不显示。此外,在我的代码所在的文件夹中还有一个名为“hs.gif”的文件。

感谢您的帮助

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    你需要移动引号:

    photo1 = PhotoImage(file="hs.gif")
    

    【讨论】:

      【解决方案2】:

      以下代码可作为您的问题的示例,也是使用图像的一种简洁方式。您还可以配置窗口的背景

      import Tkinter as tk
      from PIL import ImageTk, Image
      window = tk.Tk()
      window.title("tkinter stuff")
      window.geometry("300x300")
      window.configure(background='grey')
      path = "hs.gif"
      img = ImageTk.PhotoImage(Image.open(path))
      panel = tk.Label(window, image = img)
      panel.pack(side = "bottom", fill = "both", expand = "yes")
      window.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2020-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-19
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多