Displaying pop-up windows in Python (PyQt4)

前端 未结 1 849
感动是毒
感动是毒 2020-12-17 07:04

I need to know how to be able to make a dialog pop-up when a user clicks a button.

I\'m relatively new to both Python and PyQt/QtDesigner. I\'ve only been using in

相关标签:
1条回答
  • 2020-12-17 07:33

    So as you can see, I've connected the first button to a method named 'popup', which needs to be filled in with code to make my second window pop up. How do I go about doing this?

    Pretty much the same way you do it for your main window (MyForm).

    As usual, you write a wrapper class for your QtDesigner code for the second dialog (like you did with MyForm). Let's call it MyPopupDialog. Then in your popup method, you create an instance and then show your instance with either exec_() or show() depending whether you want a modal or modeless dialog. (If you are not familiar with Modal/Modeless concept, you might refer to the documentation.)

    So the overall thing might look like this (with a couple of modifications):

    # Necessary imports
    
    class MyPopupDialog(QtGui.QDialog):
        def __init__(self, parent=None):
            # Regular init stuff...
            # and other things you might want
    
    
    class MyForm(QtGui.QDialog):
        def __init__(self, parent=None):
            # Here, you should call the inherited class' init, which is QDialog
            QtGui.QDialog.__init__(self, parent)
    
            # Usual setup stuff
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
    
            # Use new style signal/slots
            self.ui.pushButton.clicked.connect(self.popup)     
    
            # Other things...
    
       def popup(self):
            self.dialog = MyPopupDialog()
    
            # For Modal dialogs
            self.dialog.exec_()
    
            # Or for modeless dialogs
            # self.dialog.show()
    
    if __name__ == "__main__":
       app = QtGui.QApplication(sys.argv)
       myapp= MyForm()
       myapp.show()
       sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题