简体   繁体   English

如何使用 python tkinter 将所选图像上传到 label?

[英]How can I upload the selected image into the label with using python tkinter?

I can't upload the image into the label, I tried many ways but I always keep getting an error.我无法将图像上传到 label,我尝试了很多方法,但总是出现错误。 How can I fix it?我该如何解决? I can open the file and I can select the image but I can't display it on the label我可以打开文件,我可以 select 图像,但我无法在 label 上显示它

def openImageFile():
    global imPath
    imPath = filedialog.askopenfilename(initialdir="C:\\", title = "Open an image", filetypes = (("PNG file","*.png"), ("JPEG File","*.jpeg"), ("JPG File","*.jpg"), ("All File Types","*.*")) )
    return True


def myApp(appName:str, WIDTH:int, HEIGHT:int):
    
    appWin = tk.Tk()
    appWin.title(appName)
    
    appWin.geometry(str(WIDTH)+'x'+str(HEIGHT))
    
    #FRAME
    myFrame = tk.Frame(master = appWin, bg='light gray')
    myFrame.place(relwidth = 0.98, relheight= 0.28, relx = 0.01 ,rely=0.01)
    

    btnUploadImage = tk.Button(myFrame, text="Upload Image", command = lambda: openImageFile() )
    #Butonun uygulama ekranina eklenmesi
    btnUploadImage.place(x=300,y=160)
    
    img=ImageTk.PhotoImage(Image.open(imPath))
    
    #LABEL
    firstName = tk.Label(appWin, bg='light gray', text="First Name").place(x=10,y=40)
    middleName = tk.Label(appWin, bg='light gray', text="Middle Name").place(x=10,y=70)
    surname = tk.Label(appWin, bg='light gray', text="Surname").place(x=10,y=100)
    dateOfBirth = tk.Label(appWin, bg='light gray', text="Date of Birth").place(x=10,y=130)
    photo=tk.Label(appWin, image=img).place(relwidth=0.30,relheight=0.15,x=250,y=40)

There are quite a few things you are doing wrong.你做错了很多事情。 Firstly, the place method returns None so assigning to a variable has no effect so photo=tk.Label (appWin, image=img).place(relwidth=0.30,relheight=0.15,x=250,y=40) should be首先, place方法返回None因此分配给变量没有效果,所以photo=tk.Label (appWin, image=img).place(relwidth=0.30,relheight=0.15,x=250,y=40)应该是

photo=tk.Label(appWin, image=img)
photo.place(relwidth=0.30,relheight=0.15,x=250,y=40)

in two separate lines.在两个单独的行中。 similarly for other variables.其他变量类似。

To display the image on the Label widget upon uploading, use the configure method.要在上传时在Label小部件上显示图像,请使用configure方法。

Also place img=ImageTk.PhotoImage(Image.open(imPath)) in the openImageFile function as imPath is not defined until the button is pressed. img=ImageTk.PhotoImage(Image.open(imPath))放在openImageFile function中,因为在按下按钮之前未定义 imPath。

Here is the corrected code:这是更正后的代码:

import tkinter as tk
from PIL import ImageTk, Image
from tkinter import filedialog

def openImageFile():
    global img, photo
    imPath = filedialog.askopenfilename(initialdir="C:\\", title = "Open an image", filetypes = (("PNG file","*.png"), ("JPEG File","*.jpeg"), ("JPG File","*.jpg"), ("All File Types","*.*")) )

    if imPath:
        img=ImageTk.PhotoImage(Image.open(imPath))
        photo.configure(image=img)

def myApp(appName:str, WIDTH:int, HEIGHT:int):
    global photo
    appWin = tk.Tk()
    appWin.title(appName)
    
    appWin.geometry(str(WIDTH)+'x'+str(HEIGHT))
    
    #FRAME
    myFrame = tk.Frame(master = appWin, bg='light gray')
    myFrame.place(relwidth = 0.98, relheight= 0.28, relx = 0.01 ,rely=0.01)
    

    btnUploadImage = tk.Button(myFrame, text="Upload Image", command = openImageFile)
    #Butonun uygulama ekranina eklenmesi
    btnUploadImage.place(x=300,y=160)

    #LABEL
    firstName = tk.Label(appWin, bg='light gray', text="First Name").place(x=10,y=40)
    middleName = tk.Label(appWin, bg='light gray', text="Middle Name").place(x=10,y=70)
    surname = tk.Label(appWin, bg='light gray', text="Surname").place(x=10,y=100)
    dateOfBirth = tk.Label(appWin, bg='light gray', text="Date of Birth").place(x=10,y=130)

    photo=tk.Label(appWin)
    photo.place(relwidth=0.30,relheight=0.15,x=250,y=40)

    appWin.mainloop()
    
myApp('hello', 500, 500)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM