簡體   English   中英

如何從現有窗口創建新的PyQt4窗口?

[英]How to create new PyQt4 windows from an existing window?

我一直在嘗試使用python3和Qt4從現有窗口調用一個新窗口。

我使用Qt Designer(主應用程序和另一個)創建了兩個窗口,我將Qt Designer生成的.ui文件轉換為.py腳本 - 但我似乎無法從主應用程序創建新窗口。

我試過這樣做:

############### 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選項創建可執行腳本,但它主要用於測試。

的主要目的pyuic是創建Qt的Desgner 靜態 Python模塊ui文件,讓你所包含GUI類導入應用程序。

假設您已經使用Qt Designer創建了兩個ui文件,並將它們命名為v1.uiv2.ui

然后,您將創建兩個python模塊,如下所示:

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

接下來,您將編寫一個單獨的main.py腳本,該腳本從模塊中導入GUI類,並根據需要創建它們的實例。

所以你的main.py看起來像這樣:

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類更好的名字,剛剛設置的objectName中的Qt Desgner頂級類的屬性。 在你做出改變之后別忘了重新運行pyuic

您只能創建一個QApplication 創建后,您可以創建所需的窗口數。

例如:

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QDialog):    # any super class is okay
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.button = QtGui.QPushButton('Press')
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.button.clicked.connect(self.create_child)
    def create_child(self):
        # here put the code that creates the new window and shows it.
        child = MyWindow(self)
        child.show()


if __name__ == '__main__':
    # QApplication created only here.
    app = QtGui.QApplication([])
    window = MyWindow()
    window.show()
    app.exec_()

每次單擊該按鈕都會創建一個新窗口。

您可以調整上面的示例以使用您使用Designer創建的窗口。

在旁注:

永遠不要編輯pyuic的結果。 不應更改這些文件。 這意味着:不要將mbutton1方法添加到Ui_Form

如果你有文件mywindow_ui.py ,是由pyuic創建,然后創建該文件mywindow.py ,並把這樣的事情:

from PyQt4 import QtCore, QtGui
from mywindow_ui import Ui_MyWindow

class MyWindow(QtGui.QWidget, Ui_MyWindow):   #or whatever Q*class it is
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)
    def create_child(self):   #here should go your mbutton1
        # stuff
#etc.

現在從你的主文件main.py你做到:

from PyQt4 import QtGui

from mywindow import MyWindow


# ...

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MyWindow()
    window.show()
    app.exec_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM