GUI image python

问题描述 投票:0回答:1

我被python GUI卡住了。我想从filedirector将图像传输到python GUI中的标签,但是此代码不起作用。图片无法显示在标签中。如何解决?

import os
import tkinter as tk 
from tkinter import filedialog,image_names,image_types
root = tk.Tk()

images=[]

def addImage():
    openFile.image = filedialog.askopenfile(initialdir="/",title="select image",

for image in images:
    label=tk.Label(fr1,image=openFile,bg="gray")
    label.pack()
python image tkinter label
1个回答
0
投票
  1. 在您的python库中安装Pillow。
  2. 完成后,您的导入应如下图所示from PIL import Image,ImageTk
  3. 将要传输到GUI的图像保存到与python文件相同的文件目录中

您的代码应如下所示:

`width = 50
 height = 50

 load = Image.open('my_image.png')
 load = load.resize((width,height), Image.ANTIALIAS)
 render = ImageTk.PhotoImage(load)

 img = Label(root, image=render)
 img.image = render
 img.place(x=50,y=50)`

显然,您的宽度和高度将与此处的示例不同,并且图像的位置也会有所不同。希望对您有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.