【问题标题】:Tkinter graph animation Start/Stop buttonTkinter 图形动画开始/停止按钮
【发布时间】:2020-05-05 14:43:46
【问题描述】:

所以我的图形动画效果很好,但是当我启动 GUI 时它总是在后台运行。但我只希望它在我按下“开始”按钮时启动。我已经完成了一项工作,它只是将间隔增加得非常高,而且它有点工作,但我知道它不是一个修复。而且我不确定如何使用按钮。

class PageFive(tk.Frame):

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


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


    def animate(i):
        c = app.cursor
        c.execute("SELECT time, windspeed FROM data")
        fetch = c.fetchall()

        Xaxes = [x for (x, y) in fetch]
        Yaxes = [y for (x, y) in fetch]


        pltYaxes = np.array(Yaxes)
        pltXaxes = np.array(Xaxes)

        a.clear()
        a.plot(pltXaxes,pltYaxes)


    back = tk.Button(self, text="back",height = 2, width = 13,command=lambda: controller.show_frame(PageTwo), bg='green', fg='white', font=('helvetica', 15, 'bold')) #
    back.pack()



    label = ttk.Label(self, text="Windspeed", font=LARGE_FONT)
    label.pack(pady=10,padx=10)

    canvas = FigureCanvasTkAgg(f, master=self)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    toolbar = NavigationToolbar2Tk( canvas, self )
    toolbar.update()
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    self.ani = animation.FuncAnimation(f,animate, interval=5000)

【问题讨论】:

  • 啊是的,只是复制过去的错误

标签: python tkinter graph matplotlib-animation


【解决方案1】:

所以我尝试了创建一个新的 Tkinter 窗口的选项,它似乎工作得很好。所以我只是做了一个新功能,基本上可以创建一个新的 tkinter GUI。我只是在原始 GUI 上的一个按钮中调用它。因此,当我销毁/关闭窗口时,它会停止为图形制作动画和后台工作的整个过程

def graph_window():


    root = tk.Tk()
    root.wm_title("Graph window")

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


    def animate(i):
        c = app.cursor
        c.execute("SELECT time, windspeed FROM data")
        fetch = c.fetchall()

        Xaxes = [x for (x, y) in fetch]
        Yaxes = [y for (x, y) in fetch]


        #test3 = np.array(test)
        pltYaxes = np.array(Yaxes)
        pltXaxes = np.array(Xaxes)

        a.clear()
        a.plot(pltXaxes,pltYaxes)

    canvas = FigureCanvasTkAgg(f, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)


    def _quit():
        root.quit()     # stops mainloop
        root.destroy()

    button = tk.Button(master=root, text="Quit", command=_quit)
    button.pack(side=tk.BOTTOM)
    root.ani = animation.FuncAnimation(f,animate, interval=5000)


    tk.mainloop()

所以我可以使用原始 GUI 调用此函数,例如:

AirVelocity = tk.Button(self, text="Air Velocity",command=graph_window) 

AirVelocity.grid(row = 4, column = 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2014-01-21
    • 2021-08-19
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多