【问题标题】:PYQT5 connect two QSpinBoxPYQT5 连接两个 QSpinBox
【发布时间】:2020-05-01 03:43:07
【问题描述】:
当我们改变其中一个的值时,我想知道如何将两个 QSpinBox 与条件连接起来,第二个改变了
我用 Qt 设计器试过这个
self.spinA.valueChanged['int'].connect(self.spinB.setValue)
值始终相同;我试图将标签连接到 spinA 并使用它的值来获取 spinB 的新值,但我不知道如何根据 spinB 值更改 spinA 值
对不起我的英语;我可以用我的母语更好地解释
【问题讨论】:
标签:
python-2.7
pyqt5
qspinbox
【解决方案1】:
为旋转框中的每个更改的值添加动作到第一个旋转框,在动作内部根据值之间的关系更改第二个旋转框的值,对第二个旋转框做同样的事情,下面是示例代码。
导入库
从 PyQt5.QtWidgets 导入 *
从 PyQt5 导入 QtCore、QtGui
从 PyQt5.QtGui 导入 *
从 PyQt5.QtCore 导入 *
导入系统
类窗口(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating spin box
self.spin1 = QSpinBox(self)
# setting geometry to spin box
self.spin1.setGeometry(100, 100, 150, 40)
# setting prefix to spin
self.spin1.setPrefix("Width : ")
# add action to this spin box
self.spin1.valueChanged.connect(self.action_spin1)
# creating another spin box
self.spin2 = QSpinBox(self)
# setting geometry to spin box
self.spin2.setGeometry(300, 100, 150, 40)
# setting prefix to spin box
self.spin2.setPrefix("Height : ")
# add action to this spin box
self.spin2.valueChanged.connect(self.action_spin2)
# method called after editing finished
def action_spin1(self):
# getting current value of spin box
current = self.spin1.value()
self.spin2.setValue(current)
# method called after editing finished
def action_spin2(self):
# getting current value of spin box
current = self.spin2.value()
self.spin1.setValue(current)
创建 pyqt5 应用
App = QApplication(sys.argv)
创建我们的窗口实例
窗口 = 窗口()
启动应用程序
sys.exit(App.exec())