Python:PyQt 弹出窗口 [英] Python: PyQt Popup Window

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

问题描述

所以我一直在用 Qt 为我的 Python 应用程序创建我的 GUI.我现在遇到的情况是,在按下按钮后,适当的延迟被执行,我们执行一些任务,然后我需要打开一个单独的窗口,其中包含一两件事.但我似乎无法弄清楚如何创建这个新的单独窗口.谁能给我一个如何创建一个的例子?

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?

推荐答案

一个可能让你发疯的常见错误是忘记将你创建的弹出窗口的句柄存储在一些将保持活动状态的 Python 变量中(例如在数据中主窗口的成员).

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

下面是一个简单的程序,它创建一个带有按钮的主窗口,按下按钮会打开一个弹出窗口

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)

我认为对于 Python 用户来说可能会令人惊讶,并且可能是您面临的问题是,如果您不在 main 中存储对新小部件的引用,例如通过使用 w = MyPopup(...) 而不是 self.w = MyPopup(...) 窗口显然没有出现(实际上它是创建的,它是立即销毁).

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

原因是当局部变量 w 超出范围时,因为没有人明确引用小部件,小部件将被删除.这可以清楚地看到,因为如果您再次按下按钮,您会看到第二个弹出窗口出现时第一个关闭.

The reason is that when the local variable w goes 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.

这也意味着,如果您需要创建多个弹出窗口,例如将它们放入 python 列表中,并且一旦用户关闭弹出窗口,您应该将它们从该列表中删除.示例中的等效项可以在构造函数中更改为 self.w = [],然后执行 self.w.append(MyPopup(...)).这样做可以让您打开多个弹出窗口.

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:PyQt 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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