【问题标题】:Tkinter Image not showing upTkinter 图像未显示
【发布时间】:2020-08-11 09:36:50
【问题描述】:

我对编码很陌生,我正在尝试在 tkinter 窗口中显示图像。

但是,当我运行下面的代码时,图像应该在的位置存在间隙。我也没有从这段代码中得到任何错误。

window2 = Toplevel()
window2.geometry("1920x1200")

Namearea = Label(window2, text="Please Name the Prebuild:")
Namearea.pack()

e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()

#Here is the part that is not working.
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()

SaveAndContinue = Button(window2, text="Save and Return to Main Menu", padx=75, pady=20, bg="Light Grey")
SaveAndContinue.pack()

【问题讨论】:

标签: python tkinter


【解决方案1】:

试试 josav09 对这个问题的回答: How to add an image in Tkinter?

from tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

【讨论】:

  • 它说没有名为“PIL”的模块?
  • 不是海报,但是如果你没有它,你必须通过 pip 安装它。维护的库现在称为Pillow。有关 Tkinter PhotoImage 类和 PIL 的更多详细信息,请参阅effbot.org/tkinterbook/photoimage.htm
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
相关资源
最近更新 更多