How to get combobox.currentText () value

-1

I dynamically create a combobox, but later on I can't figure out how to access it to get the current text.

Process finished with exit code -1073740791 (0xC0000409)

from form import Ui_Dialog
from PyQt5 import QtCore, QtWidgets

class MainWindow(Ui_Dialog):
    def __init__(self):
        super(MainWindow).__init__()

    def setupUi(self, Dialog):
        super(MainWindow, self).setupUi(Dialog)
        self.btn_print.clicked.connect(lambda: print_doc())

class Ui_Dialog_combobox(object):
    def __init__(self):
        super(MainWindow).__init__()
        self.comboBox = QtWidgets.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(458, 10, 151, 22))
        self.comboBox.setObjectName("comboBox_doctor")
        self.comboBox.addItem("")
        self.retranslateUi(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.comboBox.setItemText(0, _translate("Dialog", "NAME"))

def print_doc():
    name = ui.comboBox.currentText()
    print(name)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    Ui_Dialog_combobox()
    ui = MainWindow()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
python
checkbox
pyqt5
asked on Stack Overflow May 20, 2021 by Igor • edited May 20, 2021 by Igor

1 Answer

0

You seem a bit confused on how UI files should be used and how classes and instances work.

First of all, you should not subclass the "form class" alone, you must subclass from the base Qt widget class (QDialog, in this case) and the form class.

Then, the comboBox you're trying to access is probably the one you created in the Ui_Dialog_combobox, but you're just creating an instance of that class without assigning it to anything (so, it's completely useless).
Also, lots of the code in that class is problematic: the super() is incomplete (and wrong, as it refers to another class), and it also makes references to Dialog which not only is not part of that class, but is also a variable that just "happens" to be in its scope.

So, first of all, NEVER try to mimic the behavior of files generated by pyuic. Actually, you should almost always ignore their contents: you just import and use them. If you look at the official guidelines about using Designer, you'll see that there's no attempt to modify or imitate those files in any way.

Here is a (possible) proper version of your code.

from form import Ui_Dialog
from PyQt5 import QtCore, QtWidgets

class MainWindow(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        self.comboBox = QtWidgets.QComboBox(self)
        self.comboBox.setGeometry(QtCore.QRect(458, 10, 151, 22))
        self.comboBox.addItem("NAME")

        self.btn_print.clicked.connect(self.print_doc)

    def print_doc(self):
        name = self.comboBox.currentText()
        print(name)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

Some further suggestions:

  • fixed geometries are almost always a terrible idea; layout managers should be used instead;
  • function and variable names should always start with a lowercase letter, as only classes and constants normally start with an uppercase; read more about this and other important aspects on the Style Guide for Python Code;
  • use lambdas only when really required (dynamic access to local objects, etc), especially for signal connections;
  • carefully study the code above, try to understand what it does and search the documentation for all functions you don't know about;
  • pyuic generated files are instructive, so you can read them to understand what they do, and even try to modify them, but only for learning purposes; then that's it: remember, for general usage, they should never be modified, and you should not try imitate everything they do unless you really know its purpose;
answered on Stack Overflow May 20, 2021 by musicamante

User contributions licensed under CC BY-SA 3.0