Python Forum
[Tkinter] Display Selected Image from Directory and Resize it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Display Selected Image from Directory and Resize it
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,503 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  looking for scripts that do simple image display Skaperen 10 4,134 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,399 Aug-02-2021, 08:47 PM
Last Post: JayCee
  how to resize image in canvas tkinter samuelmv30 2 17,560 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  [PyQt] dynamically resize custom widget fill remaining space ironcthulhu 1 11,215 Dec-31-2019, 09:45 PM
Last Post: Denni
  8 image grid with automatical image resize by screen resolution AlexanderO 3 6,884 Dec-29-2019, 02:20 PM
Last Post: Xavier_Roga
  [PySimpleGUI] error trying to resize Text element skratt 3 8,002 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,027 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] not resize a frame Scorpio 2 3,488 Mar-13-2019, 11:24 PM
Last Post: Scorpio
  Display and update the label text which display the serial value jenkins43 5 8,995 Feb-04-2019, 04:36 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020