Kes Perron
Kes Perron

Reputation: 477

Creating a userform with PyQt5

There are lots of tutorials out there that show you how to create userforms in Python, but I can't find any which then show you how to retrieve whatever is entered into that form.

Here's an example I found where the userform looks perfect, but the buttons don't do anything:

from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,
QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,
QVBoxLayout)

import sys

class Dialog(QDialog):
    NumGridRows = 3
    NumButtons = 4

    def __init__(self):
        super(Dialog, self).__init__()
        self.createFormGroupBox()

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
#        buttonBox.accepted.connect(self.getInfo)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)

        self.setWindowTitle("Form Layout - pythonspot.com")

#    def getInfo(self):
#        shost = self.Name.text()
#        print(shost)

    def createFormGroupBox(self):
        self.formGroupBox = QGroupBox("Form layout")
        layout = QFormLayout()
        layout.addRow(QLabel("Name"), QLineEdit())
        layout.addRow(QLabel("Country"), QComboBox())
        layout.addRow(QLabel("Age"), QSpinBox())
        self.formGroupBox.setLayout(layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Dialog()
    sys.exit(dialog.exec_())

The commented lines are lines that I added that don't work. Obviously self.Name.text() isn't the right syntax, but I can't figure out what is.

Upvotes: 1

Views: 594

Answers (1)

toheedNiaz
toheedNiaz

Reputation: 1445

There are lot of things that you are missing in the code. e.g buttonBox.accepted.connect(self.accept) accept button is connected to dummy accept. you need to connect this button to your self.getInfo,

To answer why you are failing to retrieve values: you do not have object handles from your QLineEdit, QComboBox, QSpinBox which means you cannot access them later to read the updated text.

Following is the fixed code which will connect accept to self.getInfo(self) and upon pressing OK it will print the values in Name, Country, Age fields .

from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,
                             QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
                             QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,
                             QVBoxLayout)

import sys


class Dialog(QDialog):
    NumGridRows = 3
    NumButtons = 4

    def __init__(self):
        super(Dialog, self).__init__()
        self.formGroupBox = QGroupBox("Form layout")
        self.ageSpinBar = QSpinBox()
        self.countryComboBox = QComboBox()
        self.countryComboBox.addItems(["Pakistan", "USA", "UAE"])
        self.nameLineEdit = QLineEdit()
        self.createFormGroupBox()

        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.buttonBox.accepted.connect(self.getInfo)
        #        buttonBox.accepted.connect(self.getInfo)
        self.buttonBox.rejected.connect(self.reject)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)
        mainLayout.addWidget(self.buttonBox)
        self.setLayout(mainLayout)

        self.setWindowTitle("Form Layout - pythonspot.com")

    def getInfo(self):
        print("Person Name : {0}".format(self.nameLineEdit.text()))
        print("Country : {0}".format(self.countryComboBox.currentText()))
        print("Age : {0}".format(self.ageSpinBar.text()))
        self.close()

    def createFormGroupBox(self):
        layout = QFormLayout()
        layout.addRow(QLabel("Name"), self.nameLineEdit)
        layout.addRow( QLabel("Country"),self.countryComboBox)
        layout.addRow( QLabel("Age"), self.ageSpinBar)
        self.formGroupBox.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Dialog()
    sys.exit(dialog.exec_())

Note : self.close() will close the window if you don't want to close just comment this line out. Hope this helps :)

Upvotes: 3

Related Questions