【发布时间】:2021-05-11 11:48:09
【问题描述】:
我遇到了关于在 GUI 上显示所选图像的问题。当我分别运行display() 和fileopen() 这两个函数时,它工作得很好(意味着图像出现)但是当我将这两个函数放在类PageFive 中时,由于某种原因它没有。有谁知道这是为什么?
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
#Setup Menu
MainMenu(self)
#Setup Frame
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree, PageFour,PageFive):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, context):
frame = self.frames[context]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent, bg= '#e6e6e6')
#instructions
label = tk.Label(self, text="Criminal Identification System", font=("orbitron", 35, 'bold'), bg='#e6e6e6')
label.pack(pady=100,padx=100)
page_one = Button(self, text="Sign Up", command=lambda:controller.show_frame(PageOne), font=("Raleway",12 ,'bold'), bg="darkturquoise", fg="#0096aa", height=2, width=15)
page_one.pack()
page_two = Button(self, text="Sign In", command=lambda:controller.show_frame(PageTwo), font=("Raleway",12 ,'bold'), bg="darkturquoise", fg="#0096aa", height=2, width=15)
page_two.pack()
about_us = Button(self, text="About us", command=lambda:controller.show_frame(PageThree), font=("Raleway",12 ,'bold'), bg="darkturquoise", fg="#0096aa", height=2, width=15)
about_us.pack(pady=0,padx=10)
contact_us = Button(self, text="Contact us", command=lambda:controller.show_frame(PageFour), font=("Raleway",12 ,'bold'), bg="darkturquoise", fg="#0096aa", height=2, width=15)
contact_us.pack(pady=0,padx=5)
upload = Button(self, text="Upload", command=lambda:controller.show_frame(PageFive), font=("Raleway",12 ,'bold'), bg="darkturquoise", fg="#0096aa", height=2, width=15)
upload.pack(pady=0,padx=5)
class PageFive(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent, bg= '#e6e6e6')
def display():
img,panel = fileopen()
# set the image as img
panel.image = img
panel.pack()
def fileopen():
global img
# Select the Imagename from a folder
filename = filedialog.askopenfilename(title ='open', filetypes=(("PNGs", "*.png"),("JPGs", "*.jpg"), ("GIFs", "*.gif")))
# opens the image
img = Image.open(filename)
# resize the image and apply a high-quality down sampling filter
# img = img.resize((700, 500), Image.ANTIALIAS)
# PhotoImage class is used to add image to widgets, icons etc
img = ImageTk.PhotoImage(img)
# create a label
panel = Label(image = img)
return img, panel
self.button1=Button(self, text = "Browse Input Image",fg = "Black", padx=5, pady=5, bd=4, command =display)
self.button1.pack(side= 'bottom')
self.exitbutton=Button(self, text = "Exit",fg = "Black", padx=5, pady=5, bd=4, command =lambda:controller.show_frame(StartPage))
self.exitbutton.pack(side= 'bottom')
【问题讨论】:
-
只保留
PageFive相关内容后,您的代码可以正常工作。 -
抱歉,您所说的“相关内容”是什么意思?我试了很多次还是不行。
-
查看here 的精简版,只有
PageFive相关的东西才有效。
标签: python image oop user-interface tkinter