[Python-ideas] A GUI for beginners and experts alike

Jacco van Dorp j.van.dorp at deonet.nl
Tue Aug 28 03:02:11 EDT 2018


Didn't see the Qt version of the adding together with GUI yet, so here I
have a minimalist version:

import sys
from PyQt5.QtWidgets import QWidget, QSpinBox, QLabel, QApplication,
QHBoxLayout

app = QApplication(sys.argv)

w = QWidget()
w.setLayout(QHBoxLayout())
spinOne = QSpinBox(w)
spinTwo = QSpinBox(w)
output = QLabel(w)
def add():
    output.setText(str(spinOne.value() + spinTwo.value()))
spinOne.valueChanged.connect(add)
spinTwo.valueChanged.connect(add)
w.layout().addWidget(spinOne)
w.layout().addWidget(spinTwo)
w.layout().addWidget(output)
w.show()
app.exec()

And here a version how I'd actually like to see it if written seriously:

import sys
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QWidget, QSpinBox, QLabel, QApplication,
QHBoxLayout

class AddWidget(QWidget):

    newValue = pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setLayout(QHBoxLayout(self))
        self.firstInput = QSpinBox(self)
        self.layout().addWidget(self.firstInput)
        self.firstInput.valueChanged.connect(self.addTogether)
        self.secondInput = QSpinBox(self)
        self.layout().addWidget(self.secondInput)
        self.secondInput.valueChanged.connect(self.addTogether)
        self.output = QLabel(self)
        self.layout().addWidget(self.output)
        self.newValue.connect(self.output.setText)

    @pyqtSlot(int)
    def addTogether(self, arg):
        self.newValue.emit(str(self.firstInput.value() +
self.secondInput.value()))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = AddWidget()
    w.show()
    app.exec()

I'll fully admit it takes a few lines more code to actually do it right
compared to the other mentioned GUI frameworks, but it shows nicely how
it's really a toolbox full of stuff you can use to assemble what you need.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180828/f2d288b2/attachment.html>


More information about the Python-ideas mailing list