如何创建一个新的窗口按钮 PySide/PyQt? [英] How to create a new window button PySide/PyQt?

查看:28
本文介绍了如何创建一个新的窗口按钮 PySide/PyQt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Python 2.7 的 PyQt4/PySide 中遇到新窗口"函数的问题.我连接了一个 initNewWindow() 函数来创建一个新窗口、一个动作并将它放在菜单栏中.曾经是桌面软件中的常用功能.新窗口弹出并关闭,而不是在另一个窗口旁边给我一个新的持久窗口.我正在处理的代码是专有的,因此我创建了一个示例,该示例执行相同的操作,但出现以下相同的错误.有什么办法可以让它发挥作用吗?使用 Python 2.7 在 PySide 中运行.它是在 Windows 中编写和测试的.

I'm having problems with a "New Window" function in PyQt4/PySide with Python 2.7. I connected a initNewWindow() function, to create a new window, to an action and put it in a menu bar. Once a common function in desktop software. Instead of giving me a new persistent window alongside the other one the new window pops up and closes. The code I'm working on is proprietary so I created an example that does the same thing with the same error below. Is there any way to get this to work? Runs in PySide with Python 2.7. It was written in and tested in Windows.

from PySide.QtCore import QSize
from PySide.QtGui import QAction
from PySide.QtGui import QApplication
from PySide.QtGui import QLabel
from PySide.QtGui import QMainWindow
from PySide.QtGui import QMenuBar
from PySide.QtGui import QMenu
from sys import argv

def main():
    application = QApplication(argv)
    window = QMainWindow()
    window.setWindowTitle('New Window Test')
    menu = QMenuBar(window)
    view = QMenu('View')
    new_window = QAction('New Window', view)
    new_window.triggered.connect(initNewWindow)
    view.addAction(new_window)
    menu.addMenu(view)
    label = QLabel()
    label.setMinimumSize(QSize(300,300))
    window.setMenuBar(menu)
    window.setCentralWidget(label)
    window.show()
    application.exec_()


def initNewWindow():
   window = QMainWindow()
   window.setWindowTitle('New Window')
   window.show() 


if __name__ == '__main__':
   main()

推荐答案

initNewWindow() 返回时,window 变量被删除,窗口的引用计数降为零,导致新创建的 C++ 对象被删除.这就是您的窗口立即关闭的原因.

When initNewWindow() returns, the window variable is deleted and the window's reference count drops to zero, causing the newly created C++ object to be deleted. This is why your window closes immediately.

如果您想保持开放,请务必保留参考资料.最简单的方法是让你的新窗口成为调用窗口的子窗口,并设置它的 WA_DeleteOnClose 小部件属性(参见 Qt::WidgetAttribute).

If you want to keep it open, make sure to keep a reference around. The easiest way to do this is to make your new window a child of the calling window, and set its WA_DeleteOnClose widget attribute (see Qt::WidgetAttribute).

这篇关于如何创建一个新的窗口按钮 PySide/PyQt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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