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

时间:2015-04-14 13:04:25

标签: python matplotlib tkinter

我正在尝试创建一个小的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应用程序)但不完全相同。

我很感激任何关于我应该改变什么或者我应该寻求什么来源来解决这个问题的指示。

1 个答案:

答案 0 :(得分:0)

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

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

但您的代码还有其他一些问题......