-1
  1. I am trying to bind a photo to the list box, but the photo does not appear.

  2. I tried to take a specific photo path here. with the same code above (in the choosePhoto) and it worked. For some reason when in the code inside the function and is binding the function to the listBox, the photo does not appear.

My code:

from tkinter import *
from PIL import ImageTk, Image
from os import *

def openPath(path,listBox):
    try:
       path2=str(path)
       list1= listdir(path2)
       listBox.delete(0,END)
       for i in range(len(list1)):
           listBox.insert(i,list1[i])
    except:
        print("file does not exist")

def choosePhoto(event):
    path=str(textFolder.get())+"\\"+str(listBoxPath.get(ACTIVE))
    image1=ImageTk.PhotoImage(Image.open(path))
    lbl.configure(image=image1)
    print(path)


root = Tk()
root.geometry("450x600")
root.title("project image proccesor")

frame1=Frame(root,width=250,height=100)
frame1.pack(side=LEFT,fill=BOTH)
frame4=Frame(root,width=250,height=100)
frame4.pack(side=RIGHT,fill=BOTH)
lblFolder=Label(frame1,text="Enter folder path:")
lblFolder.grid(row=0,column=0)
textFolder=Entry(frame1,insertwidth=4)
textFolder.grid(rowspan=1,column=0)
listBoxPath=Listbox(frame1)
listBoxPath.grid(row=2)
bChoose=Button(frame1,text="Choose",command=lambda: openPath(textFolder.get(),listBoxPath)).grid(row=1,column=1)
lbl=Label(frame4, text="waiting for photo")
listBoxPath.bind('<<ListboxSelect>>', choosePhoto)
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 'image1' is a local variable to `choosePhoto`. You will need to define it as a global so you can keep it alive in the global namespace. – Mike - SMT Dec 31 '18 at 13:13
  • Also you are not applying a grid manager to the label so it will never show up on screen. – Mike - SMT Dec 31 '18 at 13:25
  • @Martineau. I know one of 3 of their issues is related to that duplicate post however the issue that the OP is having is more extensive. I do not think this post should be marked as a duplicate due to there being 3 distinct issues preventing the image from showing. – Mike - SMT Dec 31 '18 at 13:53

1 Answers1

0

There are 3 issues I can see here in your code.

1st. You need to define image1 as a global because this image is currently a local variable in the function and once the function completes the images is deleted unless you define it in the global namespace.

2nd. Your label that is used for displaying the images has not yet been placed on the screen. You need to use some geometry manager (probably grid()) in this case to display the image.

3rd. You are currently using ACTIVE in your selection on the list box. This will results in you selecting what was active prior to you clicking instead of what you just clicked on.

Change this:

list_box_path.get(ACTIVE)

to this:

list_box_path.get(list_box_path.curselection())

I have cleaned up your code a bit to more closely fit the PEP8 standard and added some minor changes and reduced section of code that were not needed.

import tkinter as tk
from PIL import ImageTk, Image
from os import listdir


def open_path(path):
    try:
        list1 = listdir(path)
        list_box_path.delete(0, "end")
        for i in range(len(list1)):
            list_box_path.insert(i, list1[i])
    except:
        print("file does not exist")


def choose_photo(event):
    global image1
    path = Image.open("{}\\{}".format(text_folder.get(), list_box_path.get(list_box_path.curselection())))
    image1 = ImageTk.PhotoImage(path)
    lbl.configure(image=image1)

root = tk.Tk()
root.geometry("450x600")
root.title("project image processor")

frame1 = tk.Frame(root, width=250, height=100)
frame4 = tk.Frame(root, width=250, height=100)
lbl_folder = tk.Label(frame1, text="Enter folder path:")
text_folder = tk.Entry(frame1, insertwidth=4)
list_box_path = tk.Listbox(frame1)
b_choose = tk.Button(frame1, text="Choose", command=lambda: open_path(text_folder.get()))
lbl = tk.Label(frame4, text="waiting for photo")

frame1.pack(side="left", fill="both")
frame4.pack(side="right", fill="both")
lbl_folder.grid(row=0, column=0)
text_folder.grid(rowspan=1, column=0)
list_box_path.grid(row=2)
b_choose.grid(row=1, column=1)
lbl.grid(row=0, column=0)

list_box_path.bind('<<ListboxSelect>>', choose_photo)
root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79