Bottom Page

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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] dynamically resize custom widget fill remaining space ironcthulhu 1 466 Dec-31-2019, 09:45 PM
Last Post: Denni
  8 image grid with automatical image resize by screen resolution AlexanderO 3 820 Dec-29-2019, 02:20 PM
Last Post: Xavier_Roga
  [PySimpleGUI] error trying to resize Text element skratt 3 726 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 10,193 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] not resize a frame Scorpio 2 704 Mar-13-2019, 11:24 PM
Last Post: Scorpio
  Display and update the label text which display the serial value jenkins43 5 2,607 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 891 Jan-05-2019, 01:48 PM
Last Post: wuf
  Display image in tkinter dan789 9 16,090 Dec-09-2018, 08:17 PM
Last Post: dan789
  [PyQt] Resize button with window resize FesterJester 2 3,470 Dec-03-2018, 12:02 AM
Last Post: FesterJester
  [Tkinter] Problem loading an image from directory into a Canvas object tiotony 3 1,217 Sep-02-2018, 06:47 PM
Last Post: woooee

Forum Jump:


Users browsing this thread: 1 Guest(s)