标签显示两个QSpinBox的和(Python+Pyside)?

2024-09-30 08:16:56 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我修改一下代码,在total列中显示两个微调器“Amount+Counter”的和。当前,total标签只显示单个微调器的值。我需要它来显示两个微调器的总和。由于我是python新手,所以欢迎任何其他的修复或注释。在

非常感谢。在

下面是固定的工作代码

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        #Add all GUI Elements to Class
        self.amountLabel = QtGui.QLabel('Amount')
        self.counterLabel = QtGui.QLabel('Counter')
        self.totalLabel = QtGui.QLabel('Total')
        self.amountSpin = QtGui.QSpinBox()
        self.counterSpin = QtGui.QSpinBox()

        self.totalOutput = QtGui.QLabel('0')

        grid = QtGui.QGridLayout()
        grid.setSpacing(0)

        grid.addWidget(self.amountLabel, 3, 0)
        grid.addWidget(self.counterLabel, 3, 1)
        grid.addWidget(self.totalLabel, 3, 2)

        grid.addWidget(self.amountSpin, 4, 0)
        grid.addWidget(self.counterSpin, 4, 1)
        grid.addWidget(self.totalOutput, 4, 2)

        self.setLayout(grid)

        # ACTIONS
        self.amountSpin.valueChanged[str].connect(self.onChanged)
        self.counterSpin.valueChanged[str].connect(self.onChanged)

        self.setGeometry(800, 400, 250, 80)
        self.setWindowTitle('Simple Calculator')
        self.show()

    def onChanged(self, val):
        #we ignore the val and just get the values directly from our spinboxes
        sum = self.amountSpin.value() + self.counterSpin.value()
        #and display them
        self.totalOutput.setText(str(sum))
        self.totalOutput.adjustSize()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Tags: selfmainexampledefsysgridstr微调
1条回答
网友
1楼 · 发布于 2024-09-30 08:16:56

试试这个:

#Add all GUI Elements to Class
self.amountLabel = QtGui.QLabel('Amount')
self.counterLabel = QtGui.QLabel('Counter')
self.totalLabel = QtGui.QLabel('Total')
self.amountSpin = QtGui.QSpinBox()
self.counterSpin = QtGui.QSpinBox()

[…]

^{pr2}$

[…]

def onChanged(self, val):
    #we ignore the val and just get the values directly from our spinboxes
    sum = self.amountSpin.Value + self.counterSpin.Value
    #and display them
    self.totalOutput.setText(QString(sum))
    self.totalOutput.adjustSize()

相关问题 更多 >

    热门问题