【问题标题】:How to create pages in a loop, tkinter?如何在循环中创建页面,tkinter?
【发布时间】:2020-07-19 16:29:51
【问题描述】:

我是这里的新手。我想要做的是检查文件夹中的所有图像,然后使用 tKinter 显示它。

我的以下代码实际上是从其他人那里继承的。由于它属于很多复杂的类,我不知道如何正确放置for循环。

寻找一些帮助或任何说明,或任何我可以自己学习如何做的链接

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()

在这里,我检查文件夹中的所有图像

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()

【问题讨论】:

    标签: python class tkinter


    【解决方案1】:

    如果您只想检查文件夹中的所有图像,然后使用 tKinter 显示它。那么这是最好和最简单的方法。

        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()
    

    现在您可以使用它并将该图像放入任何 tkinter 小部件中。我已将其放在列表框上,但您可以将其放在任何小部件上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 2018-08-23
      • 2018-09-02
      • 2019-04-11
      相关资源
      最近更新 更多