在Python中显示弹出窗口(PyQt4)
我需要知道如何在用户单击按钮时弹出对话框。
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与主窗口 (
MyForm
) 的操作方式几乎相同。像往常一样,您为第二个对话框的 QtDesigner 代码编写一个包装类(就像您对
MyForm
所做的那样)。我们将其命名为MyPopupDialog
。然后在您的popup
方法中,您创建一个实例,然后使用exec_()
或show()
显示您的实例,具体取决于您是否想要模态或非模态对话框。 (如果您不熟悉 Modal/Modeless 概念,您可以参考 文档。)因此总体情况可能如下所示(进行了一些修改):
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 itMyPopupDialog
. Then in yourpopup
method, you create an instance and then show your instance with eitherexec_()
orshow()
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):