按下按钮即可从所有滑块获取价值

时间:2019-03-06 06:07:52

标签: python python-3.x pyqt pyqt5

为了制作滑条,我要从使用滑条的名称,低值,高值和步长创建的另一个类中拉出它们,但是问题是我无法从滑条中拉取值。我收到属性错误,因此我很好奇是否有解决方法。也许,在滑块停止移动后存储最终的滑块位置或类似的东西?任何帮助,将不胜感激。下面是我从中获取的代码和类。

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from slider import SliderDisplay


class SpringMassDamper(QMainWindow):
def __init__(self):
    QMainWindow.__init__(self)
    self.setWindowTitle('Spring Mass Damper Simulation')

    # Main Widget
    widget = QWidget()
    self.setCentralWidget(widget)

    # Layout
    layout = QVBoxLayout()
    widget.setLayout(layout)

    # Quit button
    quit_button = QPushButton('Quit')
    quit_button.clicked.connect(app.exit)

    # Simulate System Button
    simulate_button = QPushButton('Simulate System')
    simulate_button.clicked.connect(self.print_values)

    # Labels
    self.title_1 = QLabel('System Parameters')
    self.title_1.setAlignment(Qt.AlignLeft)
    self.title_2 = QLabel('Simulation Parameters')
    self.title_2.setAlignment(Qt.AlignLeft)

    # Slider
    self.mass_slider = SliderDisplay('Mass:', 0, 10, step=1000)
    self.spring_slider = SliderDisplay('Spring:', 0, 10, step=1000)
    self.damper_slider = SliderDisplay('Damper:', 0, 10, step=1000)
    self.time_slider = SliderDisplay('Time(s):', 0, 100, step=1000)
    self.timestep_slider = SliderDisplay('Time Step(s)', 0, 10, step=1000)

    # Layout
    layout.addWidget(self.title_1)
    layout.addWidget(self.mass_slider)
    layout.addWidget(self.spring_slider)
    layout.addWidget(self.damper_slider)
    layout.addWidget(self.title_2)
    layout.addWidget(self.time_slider)
    layout.addWidget(self.timestep_slider)
    layout.addWidget(simulate_button)
    layout.addWidget(quit_button)


def print_values(self):
    x = self.mass_slider.value()
    print(x)
    #print('Mass:', float(self.mass_slider.value()))

if __name__ == '__main__':
    app = QApplication([])
    interface = SpringMassDamper()
    interface.show()
    app.exec_()



class SliderDisplay(QMainWindow):
def __init__(self, name, low, high, step=1000):
    QMainWindow.__init__(self)
    self.setWindowTitle('Slider Display')

    # Main Widget
    widget = QWidget()
    self.setCentralWidget(widget)

    # Layout
    layout = QHBoxLayout()
    widget.setLayout(layout)

    # Labels
    self.name = QLabel(str(name))
    self.label = QLabel(str(low))

    # Slider
    self.slider = QSlider(Qt.Horizontal, self)
    self.slider.setMinimum(low)
    self.slider.setMaximum(high*step)
    self.slider.setValue(low)
    self.slider.valueChanged.connect(self.change_value)

    # Layout
    layout.addWidget(self.name)
    layout.addWidget(self.label)
    layout.addWidget(self.slider)

# Function for changing value of label to the slider and accounting for step
def change_value(self):
    num = (float(self.slider.value())) / 1000
    self.label.setText(str(num))

0 个答案:

没有答案