Advertisement
RexyBadDog

tkinter_imageBrowserTest.py

Jan 27th, 2022 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. # forum link: https://www.fxp.co.il/showthread.php?t=21189240
  2. from tkinter import filedialog
  3. import tkinter as tk
  4. from PIL import ImageTk, Image
  5. import os
  6.  
  7. # create the basic window
  8. app = tk.Tk()
  9. app.title("Image")
  10. app.geometry('500x500')
  11. app.resizable(False, False)
  12.  
  13. # space for our image on Tkinter frame
  14. canvas = tk.Canvas(
  15.     app,
  16.     width = app.winfo_screenwidth()-50,
  17.     height = app.winfo_screenheight()-50
  18. )
  19.  
  20. # the open button
  21. btn1 = tk.Button(
  22.     app,
  23.     text='Click to Open File',
  24.     command=lambda: showphoto(openfn())
  25. )
  26. btn1.pack(fill=tk.X)
  27. canvas.pack()
  28. print("canvas is packed")
  29.  
  30. def openfn():
  31.     filetypes = (
  32.         ('Image files', ['*.jpg','*.jpeg','*.png','*.bmp']),
  33.         ('All files', '*.*')
  34.     )
  35.  
  36.     filename = filedialog.askopenfilename(
  37.         title='Open a file',
  38.         #initialdir='/',
  39.         initialdir=os.environ['USERPROFILE'],
  40.         filetypes=filetypes
  41.     )
  42.     return filename
  43.  
  44. def showphoto(image_path):
  45.     global image
  46.     print("image_path is: " + image_path)
  47.     image = ImageTk.PhotoImage(Image.open(f'{image_path}'))
  48.     #canvas.insert(1.0, image)
  49.     canvas.create_image(1,1,anchor=tk.constants.NW,image=image)
  50.  
  51. # run the application
  52. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement