如何在 Tkinter 中添加图像?
这给了我一个语法错误:
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
如何在 Tkinter 中添加图像?
这给了我一个语法错误:
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
2013 年 5 月 14 日 win32 上的 Python 3.3.1 [MSC v.1600 32 位(英特尔)]
按照上面的代码,这对我有用
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()
上面的代码中没有“语法错误”——它要么发生在其他行(上面不是你的所有代码,因为没有导入,也没有你的path
变量的声明),或者你得到了一些其他错误类型。
上面的例子对我来说很好,在交互式解释器上进行测试。
这是 Python 3 的示例,您可以针对 Python 2 进行编辑;)
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
root = Tk()
root.geometry("550x300+300+150")
root.resizable(width=True, height=True)
def openfn():
filename = filedialog.askopenfilename(title='open')
return filename
def open_img():
x = openfn()
img = Image.open(x)
img = img.resize((250, 250), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
panel = Label(root, image=img)
panel.image = img
panel.pack()
btn = Button(root, text='open image', command=open_img).pack()
root.mainloop()
以下代码适用于我的机器
确保你安装了 PIL 包
import Tkinter as tk
from PIL import ImageTk, Image
path = 'C:/xxxx/xxxx.jpg'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
path
根据文件指向的格式,您的实际代码可能会返回错误。话虽如此,该类已经支持某些图像格式,例如 .gif、.pgm(如果 tk.TkVersion >= 8.6 则为 .png)PhotoImage
。
下面是一个显示示例:
或者如果tk.TkVersion < 8.6
:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def download_images():
# In order to fetch the image online
try:
import urllib.request as url
except ImportError:
import urllib as url
url.urlretrieve("https://i.stack.imgur.com/IgD2r.png", "lenna.png")
url.urlretrieve("https://i.stack.imgur.com/sML82.gif", "lenna.gif")
if __name__ == '__main__':
download_images()
root = tk.Tk()
widget = tk.Label(root, compound='top')
widget.lenna_image_png = tk.PhotoImage(file="lenna.png")
widget.lenna_image_gif = tk.PhotoImage(file="lenna.gif")
try:
widget['text'] = "Lenna.png"
widget['image'] = widget.lenna_image_png
except:
widget['text'] = "Lenna.gif"
widget['image'] = widget.lenna_image_gif
widget.pack()
root.mainloop()
它不是 python 2.7 的标准库。因此,为了使它们正常工作,如果您使用的是 Python 2.7,您应该首先下载 PIL 库:直接下载链接:http://effbot.org/downloads/PIL-1.1.7.win32-py2.7。 exe 安装后,请按照以下步骤操作:
编辑你的script.py
from Tkinter import *
from PIL import ImageTk, Image
app_root = Tk()
#Setting it up
img = ImageTk.PhotoImage(Image.open("app.png"))
#Displaying it
imglabel = Label(app_root, image=img).grid(row=1, column=1)
app_root.mainloop()
希望有帮助!
此代码对我有用,您还应该考虑该窗口中是否有任何其他按钮或标签并且您不使用.place()
它将无法正常工作。
from Tkinter import*
from PIL import Image, ImageTk
img = Image.open("path/x.png")
photo=ImageTk.PhotoImage(img)
lab=Label(image=photo).place(x=50,y=50)
这是一个Python版本问题。如果您使用的是最新的,那么您的旧语法将不起作用并给您此错误。请遵循@Josav09 的代码,你会没事的。
只需将 jpg 格式的图像转换为 png 格式即可。它将 100% 工作。