Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

dialog - Displaying pop-up windows in Python (PyQt4)

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 them for about a month, but I think I have a good grasp.

Here's what I have: A main dialog (which is the main part of the application), which I designed in QtDesigner. I converted the .ui to .py using pyuic4easy.

Here's what I want to do: design a new dialog box in the QtDesigner and somehow make it pop up when a user clicks a button on the first (main) dialog.

Here's the code for my main dialog:

import sys
from PyQt4.QtCore import *
from loginScreen import *


class MyForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.popup)     
        ...

        ... Some functions ...

   def popup(self):
        #Pop-up the new dialog

if __name__ == "__main__":
   app = QtGui.QApplication(sys.argv)
   myapp= MyForm()
   myapp.show()
   sys.exit(app.exec_())

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? Remember that I already have designed my second dialog in QtDesigner, and I don't need to create a new one.

Thanks for all the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...