如何从现有窗口创建新的 PyQt4 窗口? [英] How to create new PyQt4 windows from an existing window?

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

问题描述

我一直在尝试使用 python3 和 Qt4 从现有窗口调用一个新窗口.

I've been trying to call a new window from an existing one using python3 and Qt4.

我已经使用 Qt Designer 创建了两个窗口(主应用程序和另一个),并且我已经将 Qt Designer 生成的 .ui 文件转换为 .py 脚本 - 但我似乎无法从中创建新窗口主要应用.

I've created two windows using Qt Designer (the main application and another one), and I've converted the .ui files generated by Qt Designer into .py scripts - but I can't seem to create new windows from the main application.

我尝试这样做:

############### MAIN APPLICATION SCRIPT ################

from PyQt4 import QtCore, QtGui
import v2

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(194, 101)
        self.button1 = QtGui.QPushButton(Form)
        self.button1.setGeometry(QtCore.QRect(50, 30, 99, 23))
        self.button1.setObjectName(_fromUtf8("button1"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.button1.setText(QtGui.QApplication.translate("Form", "Ventana", None, QtGui.QApplication.UnicodeUTF8))

        self.button1.connect(self.button1, QtCore.SIGNAL(_fromUtf8("clicked()")), self.mbutton1)

    def mbutton1(self):
        v2.main()



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

################## SECOND WINDOW #######################

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(160, 40, 57, 14))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "LABEL 2", None, QtGui.QApplication.UnicodeUTF8))

def main():
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

但我收到此错误消息:

 QCoreApplication::exec: The event loop is already running
 QPixmap: Must construct a QApplication before a QPaintDevice

推荐答案

虽然 pyuic 可以通过 -x, --execute 选项创建可执行脚本,但主要是用于测试.

Although pyuic can create executable scripts with the -x, --execute option, it is mainly intended for testing.

pyuic 的主要目的是从 Qt Desgner ui 文件创建 static python 模块,允许您导入 将包含的 GUI 类添加到您的应用程序中.

The main purpose of pyuic is to create static python modules from Qt Desgner ui files that allow you to import the contained GUI classes into your application.

假设您已经使用 Qt Designer 创建了两个 ui 文件,并将它们命名为 v1.uiv2.ui.

Let's say you've created two ui files using Qt Designer and named them v1.ui and v2.ui.

然后你会像这样创建两个 python 模块:

You would then create the two python modules like this:

pyuic4 -o v1.py v1.ui
pyuic4 -o v2.py v2.ui

接下来,您将编写一个单独的 main.py 脚本,从模块导入 GUI 类,并根据需要创建它们的实例.

Next, you would write a separate main.py script that imports the GUI classes from the modules, and creates instances of them as needed.

所以你的 main.py 可能看起来像这样:

So your main.py could look something like this:

from PyQt4 import QtGui
from v1 import Ui_Form1
from v2 import Ui_Form2

class Form1(QtGui.QWidget, Ui_Form1):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.button1.clicked.connect(self.handleButton)
        self.window2 = None

    def handleButton(self):
        if self.window2 is None:
            self.window2 = Form2(self)
        self.window2.show()

class Form2(QtGui.QWidget, Ui_Form2):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Form1()
    window.show()
    sys.exit(app.exec_())

请注意,为了避免命名空间冲突,我稍微更改了您的 GUI 类的名称.为了给 GUI 类提供更好的名称,只需在 Qt Desgner 中设置顶级类的 objectName 属性.并且不要忘记在进行更改后重新运行 pyuic

Note that I have changed the names of your GUI classes slightly to avoid namespace clashes. To give the GUI classes better names, just set the objectName property of the top-level class in Qt Desgner. And don't forget to re-run pyuic after you've made your changes!

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

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