标签显示两个 QSpinBox (Python + Pyside) 的总和? [英] label displays sum of two QSpinBox (Python + Pyside)?
本文介绍了标签显示两个 QSpinBox (Python + Pyside) 的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮我修复代码以在总列中显示两个微调器数量+计数器"的总和.目前总标签只显示单个微调器的值.我需要它来显示两个微调器的总和.由于我是 Python 新手,因此欢迎提供任何其他修复或注释.
Could someone help me fix the code to display in the total column the sum of the two spinners 'Amount + Counter'. Currently the total label just displays the value of a single spinner. I need it to display the sum of both spinners. Any additional fixes or notes are welcome as I'm new to 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()
推荐答案
试试这个:
#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()
[...]
# ACTIONS
self.amountSpin.valueChanged[str].connect(self.onChanged)
self.counterSpin.valueChanged[str].connect(self.onChanged)
[...]
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()
这篇关于标签显示两个 QSpinBox (Python + Pyside) 的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文