如何在不同的选项卡之间传递滑块的值?
How to pass down the slider's value across different tabs?
我目前正在使用 PyQt4 制作 GUI。我的问题如下:如何将 QtGui.QSlider
的值传递给不同的 QtGui.QTabWidget
?这是一个包含 3 个选项卡的最小工作示例:
import numpy as np
from PyQt4 import QtCore, QtGui
import matplotlib.pyplot as plt
#
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
#%%
# some Arbitrary data
nbr_points = 500
my_X_data = np.linspace(-10,10,nbr_points)
#
my_Y_data_tab1 = my_X_data**3 + 100*np.cos(my_X_data*5)
my_Y_data_tab2 = np.tan(my_X_data)
my_Y_data_tab3 = -10*my_X_data *np.sin(my_X_data)
class MyWidget(QtGui.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600,300,1000,600)
tabs = QtGui.QTabWidget()
# creation of 3 tabs
self.tab_plot1 = QtGui.QWidget()
self.tab_plot2 = QtGui.QWidget()
self.tab_plot3 = QtGui.QWidget()
#
self.setCentralWidget(tabs)
#
self.tab_plot_1()
self.tab_plot_2()
self.tab_plot_3()
tabs.addTab(self.tab_plot1, 'tab1')
tabs.addTab(self.tab_plot2, 'tab2')
tabs.addTab(self.tab_plot3, 'tab3')
self.show()
def tab_plot_1(self):
self.figure_1 = plt.figure(figsize=(15,5))
self.canvas_1 = FigureCanvas(self.figure_1)
# Slider
self.slider_1 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_1.valueChanged.connect(self.on_valueChanged_1)
ax = self.figure_1.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab1,'b-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_1 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_1)
layout.addRow(self.slider_1)
self.tab_plot1.setLayout(layout)
def tab_plot_2(self):
self.figure_2 = plt.figure(figsize=(15,5))
self.canvas_2 = FigureCanvas(self.figure_2)
# Slider
self.slider_2 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_2.valueChanged.connect(self.on_valueChanged_2)
ax = self.figure_2.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab2,'r-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_2 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_2)
layout.addRow(self.slider_2)
self.tab_plot2.setLayout(layout)
def tab_plot_3(self):
self.figure_3 = plt.figure(figsize=(15,5))
self.canvas_3 = FigureCanvas(self.figure_3)
# Slider
self.slider_3 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_3.valueChanged.connect(self.on_valueChanged_3)
ax = self.figure_3.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab3,'g-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_3 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_3)
layout.addRow(self.slider_3)
self.tab_plot3.setLayout(layout)
@QtCore.pyqtSlot(int)
def on_valueChanged_1(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab1[value,]
self.scat_1.set_offsets(np.c_[X_point,Y_point])
self.canvas_1.draw()
@QtCore.pyqtSlot(int)
def on_valueChanged_2(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab2[value,]
self.scat_2.set_offsets(np.c_[X_point,Y_point])
self.canvas_2.draw()
@QtCore.pyqtSlot(int)
def on_valueChanged_3(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab3[value,]
self.scat_3.set_offsets(np.c_[X_point,Y_point])
self.canvas_3.draw()
def main():
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在这种情况下,滑块的值应该在本地修改时跨不同的选项卡向下传递,以便在跨选项卡浏览时强制将相同的本地值传递给其他选项卡。
上次试验不成功...任何帮助将不胜感激。
如果我理解清楚,
如果滑块上的值发生变化(例如 slider_1),则将该值设置为其他两个滑块(slider_2 和 slider_3)
例如
self.slider_1.valueChanged.connect(self.on_valueChanged_1)
当 slider_1
上的值发生变化时,您将调用 self.on_valueChanged_1
,在该函数中您可以执行以下操作:
def on_valueChanged_1(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab1[value,]
self.scat_1.set_offsets(np.c_[X_point,Y_point])
self.canvas_1.draw()
# Get current value
curr_value = self.slider_1.value()
# Set the value of the slider on which the value has been changed to the other two sliders
self.slider_2.setValue(curr_value)
self.slider_3.setValue(curr_value)
并对函数 on_valueChanged_2
和 on_valueChanged_3
中的其他两个滑块重复此操作
我目前正在使用 PyQt4 制作 GUI。我的问题如下:如何将 QtGui.QSlider
的值传递给不同的 QtGui.QTabWidget
?这是一个包含 3 个选项卡的最小工作示例:
import numpy as np
from PyQt4 import QtCore, QtGui
import matplotlib.pyplot as plt
#
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
#%%
# some Arbitrary data
nbr_points = 500
my_X_data = np.linspace(-10,10,nbr_points)
#
my_Y_data_tab1 = my_X_data**3 + 100*np.cos(my_X_data*5)
my_Y_data_tab2 = np.tan(my_X_data)
my_Y_data_tab3 = -10*my_X_data *np.sin(my_X_data)
class MyWidget(QtGui.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600,300,1000,600)
tabs = QtGui.QTabWidget()
# creation of 3 tabs
self.tab_plot1 = QtGui.QWidget()
self.tab_plot2 = QtGui.QWidget()
self.tab_plot3 = QtGui.QWidget()
#
self.setCentralWidget(tabs)
#
self.tab_plot_1()
self.tab_plot_2()
self.tab_plot_3()
tabs.addTab(self.tab_plot1, 'tab1')
tabs.addTab(self.tab_plot2, 'tab2')
tabs.addTab(self.tab_plot3, 'tab3')
self.show()
def tab_plot_1(self):
self.figure_1 = plt.figure(figsize=(15,5))
self.canvas_1 = FigureCanvas(self.figure_1)
# Slider
self.slider_1 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_1.valueChanged.connect(self.on_valueChanged_1)
ax = self.figure_1.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab1,'b-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_1 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_1)
layout.addRow(self.slider_1)
self.tab_plot1.setLayout(layout)
def tab_plot_2(self):
self.figure_2 = plt.figure(figsize=(15,5))
self.canvas_2 = FigureCanvas(self.figure_2)
# Slider
self.slider_2 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_2.valueChanged.connect(self.on_valueChanged_2)
ax = self.figure_2.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab2,'r-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_2 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_2)
layout.addRow(self.slider_2)
self.tab_plot2.setLayout(layout)
def tab_plot_3(self):
self.figure_3 = plt.figure(figsize=(15,5))
self.canvas_3 = FigureCanvas(self.figure_3)
# Slider
self.slider_3 = QtGui.QSlider(minimum=0,
maximum= nbr_points-1,
orientation=QtCore.Qt.Horizontal,
tickInterval=1)
self.slider_3.valueChanged.connect(self.on_valueChanged_3)
ax = self.figure_3.add_subplot(111)
ax.clear()
ax.plot(my_X_data,my_Y_data_tab3,'g-')
ax.set_xlim([-10,10])
ax.set_ylim([-1000,1000])
X_point, Y_point = [],[]
self.scat_3 = ax.scatter(X_point, Y_point, s=100,c='k')
layout = QtGui.QFormLayout()
layout.addRow(self.canvas_3)
layout.addRow(self.slider_3)
self.tab_plot3.setLayout(layout)
@QtCore.pyqtSlot(int)
def on_valueChanged_1(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab1[value,]
self.scat_1.set_offsets(np.c_[X_point,Y_point])
self.canvas_1.draw()
@QtCore.pyqtSlot(int)
def on_valueChanged_2(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab2[value,]
self.scat_2.set_offsets(np.c_[X_point,Y_point])
self.canvas_2.draw()
@QtCore.pyqtSlot(int)
def on_valueChanged_3(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab3[value,]
self.scat_3.set_offsets(np.c_[X_point,Y_point])
self.canvas_3.draw()
def main():
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在这种情况下,滑块的值应该在本地修改时跨不同的选项卡向下传递,以便在跨选项卡浏览时强制将相同的本地值传递给其他选项卡。
上次试验不成功...任何帮助将不胜感激。
如果我理解清楚, 如果滑块上的值发生变化(例如 slider_1),则将该值设置为其他两个滑块(slider_2 和 slider_3)
例如
self.slider_1.valueChanged.connect(self.on_valueChanged_1)
当 slider_1
上的值发生变化时,您将调用 self.on_valueChanged_1
,在该函数中您可以执行以下操作:
def on_valueChanged_1(self, value):
X_point = my_X_data[value,]
Y_point = my_Y_data_tab1[value,]
self.scat_1.set_offsets(np.c_[X_point,Y_point])
self.canvas_1.draw()
# Get current value
curr_value = self.slider_1.value()
# Set the value of the slider on which the value has been changed to the other two sliders
self.slider_2.setValue(curr_value)
self.slider_3.setValue(curr_value)
并对函数 on_valueChanged_2
和 on_valueChanged_3