Chau Loi
Chau Loi

Reputation: 1225

How to create pages in a loop, tkinter?

I am a newbie here. What I want to do is to check all images in the folder then show it up using tKinter.

The below code of mine is actually inherited from others. Since it is in a lot of complicated class, I dont know how to put a for loop properly.

Looking for some helps or any instructions, or any links that I could learn how to do it by myself

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):            #Here I input the image manually
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, image = img1)    #here I manually 
       label.pack(side= "top", fill ="both", expand = True)


class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, image = img2)
       label.pack(side= "top", fill ="both", expand = True)




class MainView(tk.Frame):
    def __init__(self, image_list,*args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        print
        p1 = Page1(self)
        p2 = Page2(self)
        

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        
        
        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        
        quit_button = tk.Button(buttonframe, text = 'Quit', command = self.destroy)

        b1.pack(side="left")
        b2.pack(side="left")
        
        quit_button.pack(side="left")

        p1.show()

Here, I check all images in the folder

root = tk.Tk()

list_of_image = os.listdir()
img1 = cv2.imread(list_of_image[0])
img2 = cv2.imread(list_of_image[1])


def process_image(image):
    b,g,r = cv2.split(image)
    image = cv2.merge((r,g,b))
    im = Image.fromarray(image)
    return ImageTk.PhotoImage(image=im)

img1 = process_image(img1)
img2 = process_image(img2)


main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("800x800")
root.mainloop()

Upvotes: 0

Views: 109

Answers (1)

imxitiz
imxitiz

Reputation: 3987

If you just want to do is to check all images in the folder then show it up using tKinter. then here is best and easiest way to do it.

    from tkinter import *
    import os #If you want to open that image
    from glob import glob
    root=Tk()
    root.title("Open All files")
    def open(event):
      w=event.widget
      index=int(w.curselection()[0])
      value=w.get(index)
      os.startfile(value)#If you want to open that file

      listbox=Listbox(root,width=40,selectmode="SINGLE")
      listbox.pack(fill=BOTH,expand=1)

      for file in glob("Folder Name/*.Extension"):
        listbox.insert(END,file)
      listbox.bind('<<ListboxSelect>>',open)

      root.mainloop()

Now you can play with it and put that image in any tkinter widget.I have putted it on listbox but you can put it on any widget.

Upvotes: 1

Related Questions