图像未显示 - Tkinter



我正面临一个关于在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')

不调用fileopen(),您需要调用self.fileopen(),因为这现在是类PageFive的属性,并且您应该在fileopen函数中具有参数self,如fileopen(self)

同样,使用OOP的一个优点是避免使用全局变量,一个更好的方式来放置你的代码可能是这样的

def display(self):
self.fileopen()
self.panel.pack()
def fileopen(self):
# Select the Imagename  from a folder  
filename = filedialog.askopenfilename(title ='open', filetypes=(("PNGs", "*.png"),("JPGs", "*.jpg"), ("GIFs", "*.gif")))
# opens the image 
self.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 
self.img = ImageTk.PhotoImage(self.img) 
# create a label 
self.panel = Label(image = self.img) 
self.panel.image = self.img

编辑

即使在显示图像时,该图像也倾向于出现在帧外。

我相信这应该解决这个问题,因为现在你指定帧作为父帧。

self.panel = Label(self,image = self.img) 

仔细检查了你的代码后,我发现了其他几个问题,请参考下面更新的代码(由于不可用,我已经删除了某些行)

from tkinter import *
from tkinter import filedialog
from PIL import Image,ImageTk
class PageFive(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent, bg= '#e6e6e6')
self.pack()
self.button1=Button(self, text = "Browse Input Image",fg = "Black", padx=5, pady=5, bd=4, command =self.display)
self.button1.pack(side= 'bottom')

def display(self):
self.fileopen()
self.panel.pack()
def fileopen(self):
# Select the Imagename  from a folder  
filename = filedialog.askopenfilename(title ='open', filetypes=(("PNGs", "*.png"),("JPGs", "*.jpg"), ("GIFs", "*.gif")))
# opens the image 
self.img = Image.open(filename) 
self.img = ImageTk.PhotoImage(self.img) 
# create a label 
self.panel = Label(self,image = self.img) 
self.panel.image = self.img
root=Tk()
PageFive(root,None)
root.mainloop()

你没有打包框架,我也不认为在请求文件之后放置浏览按钮有什么意义,最好放在初始化中。

最新更新