Python Forum
[Tkinter] Display Selected Image from Directory and Resize it - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Display Selected Image from Directory and Resize it (/thread-21510.html)



Display Selected Image from Directory and Resize it - EchoLi - Oct-02-2019

Hi, I am working on a Tkinter GUI now. I would like to select an image from the directory and resize it. Now I am having trouble displaying it, even though I get the directory dialog and it is clickable.
I also want to resize the image I select, but do not really know how to put the code into the frame.

I am totally new to Python, there might be some stupid mistakes.
Thank you.

import os
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from tkinter import Label
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfile
from PIL import Image
from PIL import ImageTk




class Application(ttk.Frame):

    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.pack()

        self.button_bonus = ttk.Button(self, text="File", command=popup_bonus1)
        self.button_bonus.pack()

        self.button_another_bonus = ttk.Button(self, text="Help", command=popup_bonus2)
        self.button_another_bonus.pack()


def popup_bonus1():
    win1 = tk.Toplevel()
    win1.wm_title("File Selection")


    a = ttk.Button(win1, text="Open", command=display_selected)
    a.grid(row=0, column=0)


    b = ttk.Button(win1, text="Exit", command=root.destroy)
    b.grid(row=1, column=0)

    

def display_selected():
    Simage = askopenfile(filetypes=[("Image File","*.jpg"),("Image File","*.png")])  
    Image.open(Simage)

#def resize_image(event):
#    global hImage, hPhont, hCanvas
#    hResizedImage = hImage.resize((event.width,event.height), Image.ANTIALIAS)
#    hPhoto = ImageTk.PhotoImage(hResizedImage)
#    hCanvas.itemconfig(hItem, image=hPhoto)
#
#hImage = Image.open('D:\MNE520\FP.png')
#
#hMainWindow = tkinter.tk()
#
#hTitle = tkinter.Label(hMainWindow,text='FP')
#hTitle.pack()
#
#hCanvas = tkinter.Canvas(hMainWindow,height=hImage.size[1]+20,width=hImage.size[0]+50)
#hCanvas.pack(side='left',fill='both',expand=1)
#
#hPhoto = ImageTk.PhotoImage(hImage)
#hItem  = hCanvas.create_image(25,25,anchor='nw',image=hPhoto)
#
#hCanvas.bind('<Configure>', resize_image)
    
def popup_bonus2():
    win2 = tk.Toplevel()
    win2.wm_title("About Info")
    
    c = ttk.Button(win2, text="About", command=info)
    c.grid(row=0, column=0)
    

    
def info():
    messagebox.showinfo("About","Python Version: 3.7, Author: Echo Li, Released in 2019")


        

root = tk.Tk()
root.geometry ('300x100')
app = Application(root)

root.mainloop()