samedi 9 août 2014

Python 2.7 - affichage de l'image sélectionnée en utilisant tkinter - Stack Overflow


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 file and I have to display that image using label or something. If it is possible, I want to know how?


Thanks in advance!




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



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 file and I have to display that image using label or something. If it is possible, I want to know how?


Thanks in advance!



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 commentaires:

Enregistrer un commentaire