如何将图像添加到python GUI? [重复]

问题描述 投票:-3回答:1

这个问题在这里已有答案:

下面的代码是我的代码的一部分,它应该是一个库存管理系统。我遇到了几个问题,我将不胜感激任何帮助。

from tkinter import*
from tkinter import Tk, StringVar, ttk
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
import random
import datetime


import time;
import csv
opencsv=open('RED.csv','a')
Data=[]

LH=Label(LowerHeading, font=('arial',12,'bold'), text ="Update", bd = 10, width = 15, anchor = 'w')
LH.grid(row=1,column=0)

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
start.mainloop()

我想添加一个图像,例如,如果用户点击IDO1,将显示一件衣服的图像

该链接没有解决我的问题。我得到了一个错误,称NO模块名为PIL

python tkinter
1个回答
0
投票

你可以看看PhotoImage类。

插入图像很简单。

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

希望这可以帮助。

请查看Tkinter Labels页面中的使用标签部分中的图像。

您可以阅读Tkinter的PhotoImageLabel小部件进行自定义。

更新 - 从枕头安装'PIL' -

PIL来自包枕。使用 -

sudo pip install pillow

然后from PIL import ImageTk不会给出错误。

如果没有安装pip,请使用以下方式安装:

sudo apt-get install python-pip 
© www.soinside.com 2019 - 2024. All rights reserved.