Fekri
Fekri

Reputation: 9

How to make a file open by default with tkinter app?

I made a tkinter app for displaying images, and I was wondering if there's a way to make an image open by default with this app At the moment if I try that, I get an error for some non declared icon file (this is the icon that appears near the name of the app at the top) There's no real goal behin this Gui, I'm just experimenting and learning.

Thanks

Upvotes: 0

Views: 209

Answers (1)

Ayush Raj
Ayush Raj

Reputation: 304

Try this:

from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
my_img = []
def FileImport():
    file = filedialog.askopenfilename()
    my_img.clear()
    my_img.append(ImageTk.PhotoImage(Image.open(file)))
    label1.config(image=my_img[0])


root= tk.Tk()
root.title('Main')
root.geometry('400x400')
label = tk.Label(root, text = "Browse", fg="purple")
label.pack()
button = tk.Button(root, text='See Image',fg="blue", command=FileImport)
button.pack()
my_img.append(ImageTk.PhotoImage(Image.open(your_first_image)))
label1 = tk.Label(root, image = my_img[0])
label1.pack(pady= 50)

root.mainloop()

When you run this:

Output

After you tap to see different image from computer: After Browsing image

Hope It helps!

Upvotes: 1

Related Questions