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?

几乎和你的主窗口(MyForm)一样.

像往常一样,您为第二个对话框编写QtDesigner代码的包装类(就像您使用MyForm一样).我们称之为MyPopupDialog.然后在弹出方法中,创建一个实例,然后使用exec_()或show()显示您的实例,具体取决于您是否需要模态或无模式对话框. (如果您不熟悉Modal / Modeless概念,可以参考documentation.)

所以整体事情可能看起来像这样(有一些修改):

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