【问题标题】:Image isn't showing up - Tkinter图像未显示 - Tkinter
【发布时间】: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


【解决方案1】:

您需要调用self.fileopen(),而不是调用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()

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

【讨论】:

  • 感谢您的宝贵时间,但我尝试了此解决方案,但仍然无法正常工作!我在 App() 和 StartPage() 类中做错了吗?因为即使显示图像,图像也会出现在框架之外。
  • 如果有任何东西移动/出现在特定小部件之外,则意味着您没有正确指定父级。我可以看到标签 panel 没有指定父级,因此它将继续并假定为根。
  • 好吧,我确实做到了,现在当我运行代码(所有类)时,它甚至没有打开文件对话框窗口。它真的对你有用吗?
  • @Sam 我已经更新了答案,对我有用,希望对您有所帮助。
  • 是的,我明白为什么它不工作了,但现在它工作得很好,非常感谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
相关资源
最近更新 更多