简体   繁体   English

pyqt4:连接滑块以刷新图

[英]pyqt4: connect a slider to refresh a plot

I am trying to design a GUI to refresh a plot ie plot(timeS, dataS) , using a slider corresponding to the time vector ( timeS ) of the data that I want to display ( dataS ) which is varying in time. 我试图设计一个GUI刷新的曲线图,即plot(timeS, dataS)使用对应于时间向量(滑块timeS ,我想显示(数据) dataS其在随时间变化)。

I have tried to connect the slider to my plot function which has a lot of input self.slider.valueChanged.connect(self.myPlotFunction(.....)) , which is located in the init part of my class, but I can't use the input of the defined function myPlotFunction() . 我试图将滑块连接到我的绘图函数,该函数有很多输入self.slider.valueChanged.connect(self.myPlotFunction(.....)) ,它位于类的init部分,但是我不能使用已定义函数myPlotFunction()的输入。

So I want that when I start dragging the slider, the plot changes according to the time/position of the slider. 因此,我希望当我开始拖动滑块时,图根据滑块的时间/位置而变化。 Moreover my time vector is made of floats while the slider allows int values. 此外,我的时间向量由浮点数组成,而滑块允许使用int值。

When you connect a slot, you don't call the function, so 连接插槽时,不会调用该函数,因此

self.slider.valueChanged.connect(self.myPlotFunction(.....))

should look like 应该看起来像

self.slider.valueChanged.connect(self.myPlotFunction)

and your definition of myPlotFunction must match the argument signature of the valueChanged signal, so 并且您对myPlotFunction的定义必须与valueChanged信号的参数签名匹配,因此

def myPlotFunction(self, sliderValue):
    # Do stuff
    pass

If you need to pass more stuff to myPlotFunction , you can use a lambda, making sure to accomodate the arguments that Qt will pass first, ie 如果需要将更多内容传递给myPlotFunction ,则可以使用lambda,确保适应Qt首先传递的参数,即

otherVar = "some other stuff"
self.slider.valueChanged.connect(lambda sv, ov=otherVar: self.myPlotFunction(sv, ov))

and adjust the definition of myPlotFunction accordingly. 并相应地调整myPlotFunction的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM