displaying the selected image using tkinter

前端 未结 1 1662
花落未央
花落未央 2021-01-17 01:03

I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\\user\\desktop\\33.jpg, and I want to take only that jpg fil

相关标签:
1条回答
  • 2021-01-17 01:49

    Here is a sample code for what you are asking:

    from Tkinter import Label,Tk
    from PIL import Image, ImageTk
    import tkFileDialog
    root = Tk()
    
    path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
    im = Image.open(path)
    tkimage = ImageTk.PhotoImage(im)
    myvar=Label(root,image = tkimage)
    myvar.image = tkimage
    myvar.pack()
    
    root.mainloop()
    

    You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins. Also you might wanna add more file extensions to filetypes

    0 讨论(0)
提交回复
热议问题