[英]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. 我对Python和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. 这就是我所拥有的:一个主要对话框(这是应用程序的主要部分),我在QtDesigner中设计。 I converted the .ui to .py using pyuic4easy.
我使用pyuic4easy将.ui转换为.py。
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. 这就是我想要做的:在QtDesigner中设计一个新的对话框,当用户点击第一个(主)对话框上的按钮时,它会以某种方式弹出。
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. 正如您所看到的,我已将第一个按钮连接到名为“popup”的方法,该方法需要填入代码以弹出第二个窗口。 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.
请记住,我已经在QtDesigner中设计了第二个对话框,我不需要创建一个新对话框。
Thanks for all the help! 感谢您的帮助!
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.
正如您所看到的,我已将第一个按钮连接到名为“popup”的方法,该方法需要填入代码以弹出第二个窗口。 How do I go about doing this?
我该怎么做呢?
Pretty much the same way you do it for your main window ( MyForm
). 几乎和你的主窗口(
MyForm
)一样。
As usual, you write a wrapper class for your QtDesigner code for the second dialog (like you did with MyForm
). 像往常一样,您为第二个对话框编写QtDesigner代码的包装类(就像您使用
MyForm
)。 Let's call it MyPopupDialog
. 我们称之为
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. 然后在
popup
方法中,创建一个实例,然后使用exec_()
或show()
显示您的实例,具体取决于您是否需要模态或无模式对话框。 (If you are not familiar with Modal/Modeless concept, you might refer to the documentation. ) (如果您不熟悉Modal / Modeless概念,可以参考文档。 )
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.