Open a second window in PyQt

前端 未结 3 673
野趣味
野趣味 2021-02-02 01:23

I\'m trying to use pyqt to show a custom QDialog window when a button on a QMainWindow is clicked. I keep getting the following error:

$ python main.py 
DEBUG:          


        
3条回答
  •  清酒与你
    2021-02-02 02:18

    class StartQT4(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
    

    Why QtGui.QWidget.__init___ ??? Use insted:

    class StartQT4(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QMainWindow.__init__(self, parent)
    

    You must call __init__ methon from base class (name in parenthesis '()')

    QDialog have two useful routins:

    exec_()
    show()
    

    First wait for closing dialog and then you can access any field form dialog. Second show dialog but don't wait, so to work properly you must set some slot/signals connections to respond for dialog actions.

    eg. for exec_():

    class Dialog(QDialog):
        def __init__(self, parent):
            QDialog.__init__(parent)
            line_edit = QLineEdit()
        ...
    
    dialog = Dialog()
    if dialog.exec_():   # here dialog will be shown and main script will wait for its closing (with no errors)
        data = dialog.line_edit.text()
    

    Small tip: can you change your ui classes into widgets (with layouts). And perhaps problem is that your __init__ should be __init__(self, parent=None, dbConnection)

    Because when you create new widget in existing one PyQt may try to set it as children of existing one. (So change all init to have additional parent param (must be on second position)).

提交回复
热议问题