Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # forum link: https://www.fxp.co.il/showthread.php?t=21189240
- from tkinter import filedialog
- import tkinter as tk
- from PIL import ImageTk, Image
- import os
- # create the basic window
- app = tk.Tk()
- app.title("Image")
- app.geometry('500x500')
- app.resizable(False, False)
- # space for our image on Tkinter frame
- canvas = tk.Canvas(
- app,
- width = app.winfo_screenwidth()-50,
- height = app.winfo_screenheight()-50
- )
- # the open button
- btn1 = tk.Button(
- app,
- text='Click to Open File',
- command=lambda: showphoto(openfn())
- )
- btn1.pack(fill=tk.X)
- canvas.pack()
- print("canvas is packed")
- def openfn():
- filetypes = (
- ('Image files', ['*.jpg','*.jpeg','*.png','*.bmp']),
- ('All files', '*.*')
- )
- filename = filedialog.askopenfilename(
- title='Open a file',
- #initialdir='/',
- initialdir=os.environ['USERPROFILE'],
- filetypes=filetypes
- )
- return filename
- def showphoto(image_path):
- global image
- print("image_path is: " + image_path)
- image = ImageTk.PhotoImage(Image.open(f'{image_path}'))
- #canvas.insert(1.0, image)
- canvas.create_image(1,1,anchor=tk.constants.NW,image=image)
- # run the application
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement