图片未显示-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')

AST

除了调用之外fileopen(),您还需要调用,self.fileopen()因为这现在是类的一个属性,因此PageFive应该selffileopen函数中具有这样的参数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()

您没有打包框架,我也看不到在要求文件之后放置浏览按钮的意义,最好将它放在初始化中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章