如何在python tk的新窗口中显示路径中的图像

时间:2018-11-17 14:56:32

标签: python-3.x user-interface tkinter

浏览图像路径后,我想使用Tk库在python的下一个窗口中显示图像,但图像未在下一个窗口中显示。请在下面查看我的代码并给出答案谢谢。

import tkinter as tk
from tkinter import filedialog as fd

a=""
str1 = "e"
class Browse(tk.Frame):
    """ Creates a frame that contains a button when clicked lets the user to select
    a file and put its filepath into an entry.
    """    
    def __init__(self, master, initialdir='', filetypes=()):
        super().__init__(master)
        self.filepath = tk.StringVar()
        self._initaldir = initialdir
        self._filetypes = filetypes
        self._create_widgets()
        self._display_widgets()

    def _create_widgets(self):
        self._entry = tk.Entry(self, textvariable=self.filepath, font=("bold", 10))
        a=self._entry
        self._button = tk.Button(self, text="Browse...",bg="red",fg="white", command=self.browse)
        self._classify=tk.Button(self,text="Classify",bg="red",fg="white", command=self.classify)
        self._label=tk.Label(self, text="IMAGE CLASSIFICATION USING DEEP LERAINING.", bg="blue", fg="white",height=3, font=("bold", 14))

    def _display_widgets(self):

        self._label.pack(fill='y')
        self._entry.pack(fill='x', expand=True)
        self._button.pack(fill='y')
        self._classify.pack(fill='y')

    def retrieve_input(self):
        #str1 = self._entry.get()
        #a=a.replace('/','//')
        print (str1)

    def classify(self):
        newwin = tk.Toplevel(root)
        newwin.geometry("500x500")
        label = tk.Label(newwin, text="Classification", bg="blue", fg="white",height=3, font=("bold", 14))
        label.pack()
        canvas = tk.Canvas(newwin, height=300, width=300)
        canvas.pack()
        my_image = tk.PhotoImage(file=a, master=root)
        canvas.create_image(150, 150,  image=my_image)
        newwin.mainloop()

    def browse(self):
        """ Browses a .png file or all files and then puts it on the entry.
        """    
        self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
                                             filetypes=self._filetypes))


if __name__ == '__main__':
    root = tk.Tk()
    labelfont = ('times', 10, 'bold')
    root.geometry("500x500")
    filetypes = (
        ('Portable Network Graphics', '*.png'),
        ("All files", "*.*")
    )

    file_browser = Browse(root, initialdir=r"C:\Users",
                          filetypes=filetypes)
    file_browser.pack(fill='y')
    root.mainloop()    

1 个答案:

答案 0 :(得分:0)

您存储图像路径的全局变量a没有得到更新。您需要明确地做到这一点。下面是有效的代码。看看browse()函数。

import tkinter as tk
from tkinter import filedialog as fd

a=""
str1 = "e"
class Browse(tk.Frame):
    """ Creates a frame that contains a button when clicked lets the user to select
    a file and put its filepath into an entry.
    """
    def __init__(self, master, initialdir='', filetypes=()):
        super().__init__(master)
        self.filepath = tk.StringVar()
        self._initaldir = initialdir
        self._filetypes = filetypes
        self._create_widgets()
        self._display_widgets()

    def _create_widgets(self):
        self._entry = tk.Entry(self, textvariable=self.filepath, font=("bold", 10))
        a = self._entry
        self._button = tk.Button(self, text="Browse...",bg="red",fg="white", command=self.browse)
        self._classify=tk.Button(self,text="Classify",bg="red",fg="white", command=self.classify)
        self._label=tk.Label(self, text="IMAGE CLASSIFICATION USING DEEP LERAINING.", bg="blue", fg="white",height=3, font=("bold", 14))

    def _display_widgets(self):
        self._label.pack(fill='y')
        self._entry.pack(fill='x', expand=True)
        self._button.pack(fill='y')
        self._classify.pack(fill='y')

    def retrieve_input(self):
        #str1 = self._entry.get()
        #a=a.replace('/','//')
        print (str1)
    def classify(self):
        global a
        newwin = tk.Toplevel(root)
        newwin.geometry("500x500")
        label = tk.Label(newwin, text="Classification", bg="blue", fg="white",height=3, font=("bold", 14))
        label.pack()
        canvas = tk.Canvas(newwin, height=300, width=300)
        canvas.pack()
        my_image = tk.PhotoImage(file=a, master=root)
        canvas.create_image(150, 150, image=my_image)
        newwin.mainloop()

    def browse(self):
        """ Browses a .png file or all files and then puts it on the entry.
        """
        global a
        a = fd.askopenfilename(initialdir=self._initaldir, filetypes=self._filetypes)
        self.filepath.set(a)

if __name__ == '__main__':
    root = tk.Tk()
    labelfont = ('times', 10, 'bold')
    root.geometry("500x500")
    filetypes = (
        ('Portable Network Graphics', '*.png'),
        ("All files", "*.*")
    )

    file_browser = Browse(root, initialdir=r"~/Desktop", filetypes=filetypes)
    file_browser.pack(fill='y')
    root.mainloop()

P.S。请更改您的 initialdir 。我更改了它,因为我不在Windows上。