使用 Tkinter 选择一个文件,然后使用 Matplotlib 绘制它

Using Tkinter to choose a file, then draw it using Matplotlib

我正在尝试制作一个小型 GUI 应用程序,它允许用户选择一个 .bmp 文件,然后将给定行(在本例中为第 500 行)中的像素值显示为绘图。如果我事先给它文件名,我已经设法使情节出现,我可以从 DialogBox 中选择一个文件并让它在控制台中打印出文件名,但我对如何将文件名传递给感到困惑将重绘我的情节的功能。

import tkinter as tk
import matplotlib
from matplotlib import pyplot as plt
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

f=Figure(figsize=(5,5), dpi=100)
a=f.add_subplot(111)

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        frame = StartPage(container, self)
        self.frames[StartPage]=frame
        frame.grid(row=0,column=0,sticky="nsew")

        menu = tk.Menu(self)
        self.config(menu=menu)
         # create the file object)
        file = tk.Menu(menu)
        # adds a command to the menu option, calling it exit, and the
        # command it runs on event is client_exit
        file.add_command(label="Exit", command=self.client_exit)
        file.add_command(label="Choose_file",command=self.load_file)


        #added "file" to our menu
        menu.add_cascade(label="File", menu=file)




    def load_file(self):
        self.fname = askopenfilename(filetypes=(("Picture files", "*.png;*.bmp"),
                                               ("All files", "*.*") ))
        print(self.fname)
        return self.fname

    def client_exit(self):
        exit()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):


        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is the start page")
        label.pack(pady=10,padx=10)
        button4=tk.Button(self,text="Draw!")
        button4.pack()

#This commented out section should contain a button that should call a function that would redraw the graph once the file has been chosen. 
#I am definitely doing something wrong here.

        #button5=tk.Button(self,text="Print filename!", command=self.redraw(self.fname))
        #button5.pack()



        canvas=FigureCanvasTkAgg(f,self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=True)

def showmegraph(file):
    a.clear()
    p=np.fromfile(file,dtype=np.float16)
    p.shape=(1040,1392)
    a.plot(p[500])

app = MainWindow()
app.mainloop()

为了将它们放在一起,我一直在尝试处理这些 tutorials 他试图做一些类似的事情(一个带有 matplotlib 绘图的 GUI 应用程序)但不完全相同。

我将不胜感激任何关于我应该更改什么或我应该查看哪些来源来解决这个问题的指示。

对于您提出的具体问题,答案是:

    def load_file(self):
        self.fname = askopenfilename(filetypes=(("Picture files", "*.png;*.bmp"),
                                               ("All files", "*.*") ))
        print(self.fname)
        showmegraph(self.fname)

但是您的代码中还有其他几个问题...