从tkinter按钮内调用功能

时间:2018-04-19 16:36:29

标签: python matplotlib tkinter

我想问一下如何更改以下代码,以便在其中一个按钮点击(无关紧要)中调用animation.FuncAnimation()命令,同时仍保留切换到新页面的功能。代码取自python编程教程(sentdex),在tkinter gui中放置一个实时的matplotlib图。

我尝试了一些方法,例如将函数调用放在按钮声明中,创建一个新函数,包含显示框架和启动动画的功能,并从按钮调用该函数。我失败了几次,并希望得到建议。提前谢谢。

    import matplotlib matplotlib.use("TkAgg") 
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
    from matplotlib.figure import Figure 
    import matplotlib.animation as animation 
    from matplotlib import style 
    import tkinter as tk 
    from tkinter import ttk 

    LARGE_FONT=("Verdana", 12) 
    style.use("ggplot") 
    f=Figure(figsize=(5,5), dpi=100) 
    a = f.add_subplot(111) 

    def animate(i): 
        pullData = open("sampleData.txt","r").read() 
        dataList = pullData.split('\n') 
        xList = [] 
        yList = [] 
        for eachLine in dataList: 
            if len(eachLine)>1: 
                x,y = eachLine.split(',') 
                xList.append(int(x)) 
                yList.append(int(y)) 
                a.clear() 
                a.plot(xList,yList) 

    class SeaofBTCapp (tk.Tk): 

        def _init_(self,*args,**kwargs): 

            tk.Tk.__init__(self, *args, **kwargs) 
            tk.Tk.iconbitmap(self, default="icon.ico") 
            tk.Tk.wm_title(self, "Sea of BTC client") 
            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 = {} 

            for F in (StartPage, PageOne, PageTwo, PageThree): 
                frame = F(container, self) 
                self.frames[F] = frame 
                frame.grid(row=0, column = 0, sticky="nsew")
            self.show_frame(StartPage) 

         def show_frame(self,cont): 
             frame = self.frames[cont] 
             frame.tkraise() 

         def qf(): 
             print("Hamne kar dikhaya") 

    class StartPage(tk.Frame): 

        def _init_(self, parent, controller):

            tk.Frame.__init__(self,parent) 
            label = tk.Label(self, text="Start Page", font=LARGE_FONT)
            label.pack(pady=10, padx=10) 
            button = ttk.Button(self, text="Visit Page 1", command=lambda :controller.show_frame(PageOne)) 
            button.pack() 
            buttona = ttk.Button(self, text="Visit Page 2", command=lambda :controller.show_frame(PageTwo)) 
            buttona.pack() 
            buttonb = ttk.Button(self, text="Visit Graph Page", command=lambda :controller.show_frame(PageThree)) 
            buttonb.pack() 

    class PageOne(tk.Frame): 

        def _init_(self, parent, controller): 

            tk.Frame.__init__(self,parent) label = tk.Label(self, text="Page One", font=LARGE_FONT) 
            label.pack(pady=10, padx=10) button1 = ttk.Button(self, text="Back to Home", command=lambda :controller.show_frame(StartPage))
            button1.pack() 

    class PageTwo(tk.Frame): 

        def _init_(self, parent, controller): 

            tk.Frame.__init__(self,parent) 
            label = tk.Label(self, text="Page Two", font=LARGE_FONT) label.pack(pady=10, padx=10) 
            button1 = ttk.Button(self, text="Back to Home", command=lambda :controller.show_frame(StartPage)) 
            button1.pack() 

    class PageThree(tk.Frame): 

        def _init_(self, parent, controller):

            tk.Frame.__init__(self,parent) 
            label = tk.Label(self, text="Graph Page", font=LARGE_FONT) label.pack(pady=10, padx=10) 
            button1 = ttk.Button(self, text="Back to Home", command=lambda :controller.show_frame(StartPage)) 
            button1.pack() 
            canvas = FigureCanvasTkAgg(f,self) 
            canvas.show() 
            canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True) 

            toolbar = NavigationToolbar2TkAgg(canvas, self) 
            toolbar.update() 
            canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    app= SeaofBTCapp() 
    ani = animation.FuncAnimation(f,animate, interval=1000) 
    app.mainloop()

0 个答案:

没有答案