【发布时间】: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()
【问题讨论】: