Python:PyQt 弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4838890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: PyQt Popup Window
提问by Morphine
So I've been creating my GUI with Qt for my Python application. I've now come to a situation where after a button has been pushed the appropriate deferred gets executed, we perform some tasks then I need to open up a separate window that contains one or two things. But I can't seem to figure out how to create this new separate window. Could anyone give me an example of how to create one?
所以我一直在用 Qt 为我的 Python 应用程序创建我的 GUI。我现在遇到的情况是,在按下按钮后,执行适当的延迟执行,我们执行一些任务,然后我需要打开一个单独的窗口,其中包含一两件事。但我似乎无法弄清楚如何创建这个新的单独窗口。谁能给我一个如何创建一个的例子?
采纳答案by 6502
A common error that can drive you crazy is forgetting to store the handle of the popup window you create in some python variable that will remain alive (e.g. in a data member of the main window).
一个让你发疯的常见错误是忘记将你创建的弹出窗口的句柄存储在一些将保持活动状态的 python 变量中(例如,在主窗口的数据成员中)。
The following is a simple program that creates a main window with a button where pressing the button opens a popup
下面是一个简单的程序,它创建一个带有按钮的主窗口,按下按钮会打开一个弹出窗口
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
from PyQt4.Qt import *
class MyPopup(QWidget):
def __init__(self):
QWidget.__init__(self)
def paintEvent(self, e):
dc = QPainter(self)
dc.drawLine(0, 0, 100, 100)
dc.drawLine(100, 0, 0, 100)
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.cw = QWidget(self)
self.setCentralWidget(self.cw)
self.btn1 = QPushButton("Click me", self.cw)
self.btn1.setGeometry(QRect(0, 0, 100, 30))
self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
self.w = None
def doit(self):
print "Opening a new popup window..."
self.w = MyPopup()
self.w.setGeometry(QRect(100, 100, 400, 200))
self.w.show()
class App(QApplication):
def __init__(self, *args):
QApplication.__init__(self, *args)
self.main = MainWindow()
self.connect(self, SIGNAL("lastWindowClosed()"), self.byebye )
self.main.show()
def byebye( self ):
self.exit(0)
def main(args):
global app
app = App(args)
app.exec_()
if __name__ == "__main__":
main(sys.argv)
What I think can be surprising for Python users and may be is the problem you are facing is the fact that if you don't store a reference to the new widget in the main e.g. by using w = MyPopup(...)instead of self.w = MyPopup(...)the window apparently doesn't appear (actually it's created and it's immediately destroyed).
我认为对于 Python 用户来说可能会令人惊讶,并且可能是您面临的问题是,如果您不在主中存储对新小部件的引用,例如使用w = MyPopup(...)而不是self.w = MyPopup(...)窗口显然不会出现(实际上它被创建并立即被销毁)。
The reason is that when the local variable wgoes out of scope as no one is explicitly referencing the widget the widget gets deleted. This can be seen clearly because if you press again the button you'll see that as the second popup appears the first one is closed.
原因是当局部变量w超出范围时,因为没有人明确引用小部件,小部件将被删除。这可以清楚地看到,因为如果您再次按下按钮,您会看到第二个弹出窗口出现时第一个关闭。
This also means that if you need to create several popups you have for example to put them in a python list and you should remove them from this list once the popups are closed by the user. The equivalent in the example could be changing to self.w = []in constructor and then doing self.w.append(MyPopup(...)). Doing that would allow you to open several popups.
这也意味着,如果您需要创建多个弹出窗口,例如,您必须将它们放入一个 python 列表中,并且一旦用户关闭弹出窗口,您应该将它们从该列表中删除。示例中的等效项可以更改为self.w = []in 构造函数,然后执行self.w.append(MyPopup(...)). 这样做将允许您打开多个弹出窗口。
回答by flying sheep
Generally, you just show multiple parentless windows with someQWidget.show(), like:
通常,您只需使用 显示多个无父窗口someQWidget.show(),例如:
w1 = QLabel("Window 1")
w2 = QLabel("Window 2")
w1.show()
w2.show()
But most likely, you want a modal standard Dialog like this. Also be sure to understand modal dialogs.

