Python Matplotlib滑块小部件未更新

时间:2012-02-17 23:18:56

标签: python slider matplotlib

我想使用多个包含数据+ a(matplotlib)滑块小部件的matplotlib画布。 问题是滑块小部件没有正确更新(看起来没有发送鼠标事件或其他东西)

这就是我所拥有的:

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.widgets import Slider

class PlotWidget( Tk.Frame ):

    def __init__(self, master):
        Tk.Frame.__init__(self, master)
        self.figure = Figure(figsize=(5,4), dpi=75)
        self.canvas = FigureCanvasTkAgg(self.figure, self)

        self.frame = self.canvas.get_tk_widget()
        self.frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        self.canvas.show()

    def add_slider(self):
       a = self.figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')
       s = Slider( a, 'range', 0.1, 30.0, valinit=5)
       self.canvas.show()



root = Tk.Tk()

option = 1    

if option == 1 or option = 2:
     w =  PlotWidget(root)
     w.pack()
     figure = w.figure
else:
     f = Tk.Frame(root, bd = 6, bg='red')
     figure = matplotlib.figure.Figure(figsize=(5,4), dpi=75)
     canvas = FigureCanvasTkAgg(figure, f)
     canvas.get_tk_widget().pack()
     canvas.show()
     f.pack()


if option = 1:
    w.add_slider()
else:
    a = figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')   
    s = Slider( a, 'range', 0.1, 30.0, valinit=5)

Tk.mainloop()

选项1不能正常工作所有其他组合(至少在测试脚本中,在我的应用程序中,当我在另一个位置创建滑块小部件时,它也不起作用。 滑块对鼠标事件没有反应.....

我错过了什么?!

1 个答案:

答案 0 :(得分:2)

我必须进行以下更改才能让您的脚本在我的计算机上运行。

import matplotlib
from Tkinter import *
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.widgets import Slider

class PlotWidget( Frame ):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.figure = Figure(figsize=(5,4), dpi=75)
        self.canvas = FigureCanvasTkAgg(self.figure, self)

        self.frame = self.canvas.get_tk_widget()
        self.frame.pack(side=TOP, fill=BOTH, expand=1)
        self.canvas.show()

    def add_slider(self):
       a = self.figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')
       s = Slider( a, 'range', 0.1, 30.0, valinit=5)
       self.canvas.show()



root = Tk()

option = 1

if option == 1 or option == 2:
     w =  PlotWidget(root)
     w.pack()
     figure = w.figure
else:
     f = Frame(root, bd = 6, bg='red')
     figure = matplotlib.figure.Figure(figsize=(5,4), dpi=75)
     canvas = FigureCanvasTkAgg(figure, f)
     canvas.get_tk_widget().pack()
     canvas.show()
     f.pack()


if option == 1:
    w.add_slider()
else:
    a = figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')   
    s = Slider( a, 'range', 0.1, 30.0, valinit=5)

root.mainloop()

我认为由于创建Slider的范围,选项1不起作用。

示例here也在与图和show命令相同的范围内创建它。

如果你想将Slider创建移动到一个函数中,你有该函数将Slider返回到函数外的范围,添加为add_slider return s的最后一行,然后将w.add_slider()更改为s = w.add_slider()

或者您可以将相关内容存储在与画布和图形相同的范围(类成员)中,例如self.slider = Slider( a, 'range', 0.1, 30.0, valinit=5)

这两种方法都会在option = 1时对鼠标事件进行滑块更新。