【问题标题】:How to embed this matplotlib code into tkinter canvas?如何将此 matplotlib 代码嵌入到 tkinter 画布中?
【发布时间】:2011-04-03 00:37:17
【问题描述】:

matplotlib 代码在http://matplotlib.sourceforge.net/examples/animation/strip_chart_demo.html

我想将此代码嵌入到我的 tkinter GUI 中,我应该怎么做?正如它所说“”“这个例子使用了 gtk,但并不密切依赖它。它只是使用空闲处理程序来触发事件。你可以将它插入一个支持动画的不同 GUI(GTKAgg,TkAgg , WXAgg) 并使用 您的工具包空闲/定时器功能。"""

TkAgg 中的空闲/定时器功能是什么?

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    这可能是也可能不是您想要的:

    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure
    from matplotlib.lines import Line2D
    import numpy as np
    
    try:
        import Tkinter as Tk
    except ImportError:
        import tkinter as Tk
    
    
    class Scope:
        def __init__(self, ax, maxt=10, dt=0.01):
            self.ax = ax
            self.canvas = ax.figure.canvas
            self.dt = dt
            self.maxt = maxt
            self.tdata = [0]
            self.ydata = [0]
            self.line = Line2D(self.tdata, self.ydata, animated=True)
            self.ax.add_line(self.line)
            self.background = None
            self.canvas.mpl_connect('draw_event', self.update_background)
            self.ax.set_ylim(-.1, 1.1)
            self.ax.set_xlim(0, self.maxt)
    
        def update_background(self, event):
            self.background = self.canvas.copy_from_bbox(self.ax.bbox)
    
        def emitter(self, p=0.01):
            'return a random value with probability p, else 0'
            v = np.random.rand(1)
            if v>p: return 0.
            else: return np.random.rand(1)
    
        def update_view(self, *args):
            if self.background is None: return True
            y = self.emitter()
            lastt = self.tdata[-1]
            if lastt>self.tdata[0]+self.maxt:
                self.tdata = [self.tdata[-1]]
                self.ydata = [self.ydata[-1]]
                self.ax.set_xlim(self.tdata[0], self.tdata[0]+self.maxt)
                self.ax.figure.canvas.draw()
    
            self.canvas.restore_region(self.background)
    
            t = self.tdata[-1] + self.dt
            self.tdata.append(t)
            self.ydata.append(y)
            self.line.set_data(self.tdata, self.ydata)
            self.ax.draw_artist(self.line)
    
            self.canvas.blit(self.ax.bbox)
            self.canvas.get_tk_widget().after(25, self.update_view) # ***important
            return True
    
    
    root = Tk.Tk()
    
    f = Figure()
    ax = f.add_subplot(111)
    
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
    canvas.show()
    
    toolbar = NavigationToolbar2TkAgg( canvas, root )
    toolbar.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
    toolbar.update()
    
    button = Tk.Button(master=root, text='Quit', command=root.destroy)
    button.pack(side=Tk.BOTTOM)
    
    scope = Scope(ax)
    root.after(50, scope.update_view)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2019-10-07
      • 2019-04-08
      • 2020-10-29
      相关资源
      最近更新 更多