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
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 2,048 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] not resize a frame Scorpio 2 372 Mar-13-2019, 11:24 PM
Last Post: Scorpio
  Display and update the label text which display the serial value jenkins43 5 979 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 579 Jan-05-2019, 01:48 PM
Last Post: wuf
  Display image in tkinter dan789 9 6,230 Dec-09-2018, 08:17 PM
Last Post: dan789
  [PyQt] Resize button with window resize FesterJester 2 1,517 Dec-03-2018, 12:02 AM
Last Post: FesterJester
  [Tkinter] Problem loading an image from directory into a Canvas object tiotony 3 794 Sep-02-2018, 06:47 PM
Last Post: woooee
  Tkinter Image Display Weird Tearing Upon Transforming Emerest 6 1,235 Sep-01-2018, 11:46 PM
Last Post: Emerest

Forum Jump:


Users browsing this thread: 1 Guest(s)