在Python中显示弹出窗口(PyQt4)

发布于 2025-01-01 02:07:13 字数 976 浏览 0 评论 0原文

我需要知道如何在用户单击按钮时弹出对话框。

我对 Python 和 PyQt/QtDesigner 都比较陌生。我只使用了大约一个月,但我认为我已经掌握得很好。

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

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

这是我的主对话框的代码:

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

如您所见,我已将第一个按钮连接到名为“popup”的方法,该方法需要填充代码才能弹出第二个窗口。我该怎么做呢?请记住,我已经在 QtDesigner 中设计了第二个对话框,并且不需要创建新对话框。

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-08 02:07:13

如您所见,我已将第一个按钮连接到名为的方法
“弹出窗口”,需要填写代码才能创建第二个
弹出窗口。我该如何去做呢?

与主窗口 (MyForm) 的操作方式几乎相同。

像往常一样,您为第二个对话框的 QtDesigner 代码编写一个包装类(就像您对 MyForm 所做的那样)。我们将其命名为MyPopupDialog。然后在您的 popup 方法中,您创建一个实例,然后使用 exec_()show() 显示您的实例,具体取决于您是否想要模态或非模态对话框。 (如果您不熟悉 Modal/Modeless 概念,您可以参考 文档。

因此总体情况可能如下所示(进行了一些修改):

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

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_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文