1

How do I use user input to retrieve an image, and display it on the screen? I have the user input field in Tkinter, and I do not know how to get that to go to my folder, and get the image to show up on the same screen.

EDIT: I was able to get the path to show on the screen, but now I don't know how to get the actual image to show up from that path inside the frame... I do not know how to link this path to have the program display the image, i.e. myimage1 from my folder.

from tkinter import *
from PIL import ImageTk,Image
from tkinter import ttk
from tkinter import filedialog

root = Tk()
root.title("Images")
root.iconbitmap(r'C:\Users\hadhikari\example\Takumi_Logo.ico')

button_quit = Button(root, text= "Exit Program", command=root.quit)
button_quit.pack()

e= Entry(root, width=50, bg="light blue", borderwidth=3)
e.pack()
e.insert(0," ")


def myClick():
    link = "r'C:\\Users\\hadhikari\\example\\" + e.get()
    myLabel1 = Label(root, text=link)
    myLabel1.pack()


myButton = Button(root, text="Scan Part Number", command=myClick, 
bg="pink", fg="white")
myButton.pack()

#my_img = ImageTk.PhotoImage(Image.open('link'))
#my_Label = Label(image=my_img)
#my_Label.pack()

#def getText():                                                         
#    inputtedtext = entrybox.get()

#entrybox = Entry(root)
#entrybox.pack()

#btn = Button(root, text="Submit", command=getText)
#btn.pack()

frame = LabelFrame(root, padx=100, pady=100)
frame.pack(padx=50, pady=50)

root.mainloop()
HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • Please add an example of what you want and what you got so far, it would be easier to help you – Viper Jul 21 '21 at 13:55
  • Can you show us what you have tried? Also look at `tkinter.filedialog.askopenfilename` and `tkinter.PhotoImage` – TheLizzard Jul 21 '21 at 13:55
  • @Viper. I have updated the questions panel. – Heman Adhikari Jul 21 '21 at 15:29
  • @TheLizzard I have updated my questions panel with the code I have currently. – Heman Adhikari Jul 21 '21 at 15:31
  • When do you want the user to pick the file? When the program is starting up or do you want to add a button for it? Try adding `from tkinter.filedialog import askopenfilename` at the start of the code and then use `filename = askopenfilename()` where ever you need the filename – TheLizzard Jul 21 '21 at 15:34
  • @TheLizzard i tried the above code and it does open up the folder where the file is. so, now my issue is... I want to be able to start the application and with in the application i want the user to put in the name of the image file. eg: mypicture1 and i want the program to go to that folder and grab mypicture1 and display it within the frame. i think i know what i need but i am stuck :( i might be asking the wrong question.... – Heman Adhikari Jul 21 '21 at 18:37

1 Answers1

0

As suggested in the comments, maybe using Tkinter's askopenfilename dialog might be a better choice than hardcoding the filepath, and letting the user input the filename.

But, first of all, let's fix the code following your intentions. Most of the part on how to properly set up a Tkinter Label, and dynamically place images in that, can be easily found, for example in this Q&A. Basically, you want to put the "update routine" inside your myClick method:

from tkinter import *
from PIL import Image, ImageTk

# How to properly set up Tkinter label with dynamically changeable
# ImageTk.PhotoImage: https://stackoverflow.com/a/3482156/11089932

root = Tk()

button_quit = Button(root, text='Exit Program', command=root.quit)
button_quit.pack()

e = Entry(root, width=50, bg='light blue', borderwidth=3)
e.pack()
e.insert(0, '')

my_label = Label()
my_label.pack()


def myClick():
    link = r'your/path/goes/here' + e.get()
    my_img = ImageTk.PhotoImage(Image.open(link))
    my_label.configure(image=my_img)
    my_label.image = my_img


myButton = Button(root, text='Scan Part Number', command=myClick,
                  bg='pink', fg='white')
myButton.pack()

root.mainloop()

Program at startup:

Startup

Opening the first image:

First image

Opening another image:

Another image

Now, regarding the askopenfilename dialog, we get rid of the Entry widget, and simply open the dialog inside the myClick method:

from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk

# How to properly set up Tkinter label with dynamically changeable
# ImageTk.PhotoImage: https://stackoverflow.com/a/3482156/11089932

root = Tk()

button_quit = Button(root, text='Exit Program', command=root.quit)
button_quit.pack()

my_label = Label()
my_label.pack()


def myClick():
    link = askopenfilename()
    my_img = ImageTk.PhotoImage(Image.open(link))
    my_label.configure(image=my_img)
    my_label.image = my_img


myButton = Button(root, text='Scan Part Number', command=myClick,
                  bg='pink', fg='white')
myButton.pack()

root.mainloop()

Program at startup:

Startup

Opening the first image (dialog):

File dialog

Opening the first image (display):

First image

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.19041-SP0
Python:        3.9.1
PyCharm:       2021.1.3
Pillow:        8.3.1
----------------------------------------
HansHirse
  • 18,010
  • 10
  • 38
  • 67