在Python中显示弹出窗口(PyQt4) [英] Displaying pop-up windows in Python (PyQt4)

查看:1105
本文介绍了在Python中显示弹出窗口(PyQt4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击按钮时,我需要知道如何使对话框弹出。

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

我对Python和PyQt / QtDesigner来说都比较新。我只在他们当中使用了大约一个月,但我觉得我有一个很好的把握。

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.

这是我的主要对话框(主要是对话框部分应用程序),我在QtDesigner中设计。我使用pyuic4easy将.ui转换为.py。

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.

这是我要做的:在QtDesigner中设计一个新的对话框,并以某种方式使其弹出,当用户点击第一个(主)对话框上的按钮。

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

所以你可以看到,我已经将第一个按钮连接到一个名为弹出的方法,该方法需要用代码填充才能使我的第二个窗口弹出。我该怎么做呢?记住,我已经在QtDesigner中设计了我的第二个对话框,我不需要创建一个新的对话框。

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.

感谢所有的帮助!

推荐答案


所以你可以看到,我已将第一个按钮连接到一个名为
'弹出窗口的方法',这需要填写代码,使我的第二个
窗口弹出。我如何去做这件事?

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 )。

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

像往常一样,为第二个对话框的QtDesigner代码编写一个包装类(就像你在 MyForm的)。我们称之为 MyPopupDialog 。然后在您的弹出方法中,创建一个实例,然后使用 exec _() show()根据您是否需要模态或无模式对话框。 (如果您不熟悉Modal / Modeless概念,可以参考文档

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

这篇关于在Python中显示弹出窗口(PyQt4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆