首页 > 解决方案 > PYQT5 连接两个 QSpinBox

问题描述

我想知道当我们更改其中一个的值时如何将两个 QSpinBox 与条件连接,第二个更改的值我使用 Qt 设计器尝试过

self.spinA.valueChanged['int'].connect(self.spinB.setValue)

值始终相同;我试图将标签连接到 spinA 并使用它的值来获取 spinB 的新值,但我不知道如何根据 spinB 值更改 spinA 值,对不起我的英语;我可以用我的母语更好地解释

在此处输入图像描述

在此处输入图像描述

标签: python-2.7pyqt5qspinbox

解决方案


为旋转框中的每个更改的值添加动作到第一个旋转框,在动作内部根据值之间的关系更改第二个旋转框的值,对第二个旋转框执行相同的操作,下面是示例代码。

导入库

from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtCore import *

导入系统

类窗口(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 应用程序

应用程序 = QApplication(sys.argv)

创建我们的窗口实例

窗口 = 窗口()

启动应用程序

sys.exit(App.exec())


推荐阅读