将 PyQt5 spinbox 链接到定义的 push Botton 下的变量 [英] Link PyQt5 spinbox to a variables under defined push Botton

查看:67
本文介绍了将 PyQt5 spinbox 链接到定义的 push Botton 下的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个旋转框链接到一个变量 A1、A2、L,这些变量将用作计算公式中的整数输入.旋转框的目的是供用户用来输入所需的计算值.我无法链接它们.任何想法表示赞赏.

I want to link a spinbox to a variable A1, A2, L that will be used as integer input in a formula for calculation. The purpose of the spin box is to be used by a user to enter the desired value for calculation. I am not able to link them. Any ideas are appreciated.

self.pushButton.clicked.connect(self.pushButton_handler)
self.spinBox.valueChanged.connect(A1)
self.spinBox_2.valueChanged.connect(A2)
self.spinBox_3.valueChanged.connect(L) 


def pushButton_handler(self):

                A1 = self.spinBox.value
                A2 = self.spinBox_2.value
                L= self.spinBox_3.value
                F=1

                I=A1+A2-L+F
                print ('I')

推荐答案

该属性保存旋转框的值:

This property holds the value of the spin box:

QtWidgets.QSpinBox.value()       

Events QSpinBox 将信号 valueChanged 绑定到插槽,在您的情况下为 self.pushButton_handler

Events QSpinBox binds the signal valueChanged to the slot, in your case self.pushButton_handler

import sys
from PyQt5.Qt import *

class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.spinBox = QSpinBox()
        self.spinBox.setRange(1, 200)
        self.spinBox.setValue(11)
        self.spinBox.setPrefix(" A1 :  ")
        self.spinBox.valueChanged.connect(self.pushButton_handler)

        self.spinBox_2 = QSpinBox()
        self.spinBox_2.setRange(1, 200)
        self.spinBox_2.setValue(55)
        self.spinBox_2.setPrefix(" A2 :  ")
        self.spinBox_2.valueChanged.connect(self.pushButton_handler)

        self.spinBox_3 = QSpinBox()
        self.spinBox_3.setRange(1, 200)
        self.spinBox_3.setValue(77)
        self.spinBox_3.setPrefix(" L_ :  ")        
        self.spinBox_3.valueChanged.connect(self.pushButton_handler)

        self.pushButton = QPushButton("Click me", clicked=self.pushButton_handler)

        self.label = QLabel('')

        lay = QVBoxLayout(self)
        lay.addWidget(self.spinBox)
        lay.addWidget(self.spinBox_2)
        lay.addWidget(self.spinBox_3)
        lay.addWidget(self.pushButton)
        lay.addWidget(self.label)


    def pushButton_handler(self):
        a1 = self.spinBox.value()                # + ()
        a2 = self.spinBox_2.value()              # + ()
        l  = self.spinBox_3.value()              # + ()
        f  = 1

        i = a1 + a2 - l + f

#        print ('a1 + a2 - l + f = {}'.format(i))  
        self.label.setText('a1 + a2 - l + f = {}'.format(i))   


if __name__ == "__main__":
    app = QApplication(sys.argv)
    default_font = QFont()
    default_font.setPointSize(15)
    app.setFont(default_font)
    w = Example()
    w.resize(220, 200)
    w.show()
    sys.exit(app.exec_())

这篇关于将 PyQt5 spinbox 链接到定义的 push Botton 下的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆