Python 2.7 PyQt4将主窗口文件添加到另一个主窗口的选项卡

时间:2016-11-12 04:12:38

标签: python-2.7 pyqt4 qwidget qtabwidget qgridlayout

我正在使用Python 2.7创建一个程序,并且遇到了一个问题,那就是几个小时的搜索谷歌和论坛都没有回答。

我的程序包含一个名为sessions_window.py的Main_Window,它包含一个Tab Widget。在每个选项卡中,应包含单独的主窗口文件。例如,sessions_window.py的一个标签是帐户标签,我有另一个包含帐户信息的acount1.py文件,我希望将其嵌入帐户标签中(是的,我知道我拼写帐户"帐户& #34;,我稍后会解决)。另一个选项卡将被称为Graph,并且应该包含另一个Graph.py文件(尽管我还没有达到这一点)。

*作为一个注释,我根据索引/描述系统命名了我的元素,所以它们有点长而且时髦但它对我有帮助。

我已经成功创建了这些文件,但我仍然坚持将它们嵌入到session_window.py标签中。

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import acount1

class Ui_BxSession(object):

  def configurePageB(self, BxSession):

        self.accountTabImport = acount1.Ui_MainWindow

# B : Session Page
        BxSession.setObjectName("BxSession")
        BxSession.resize(800, 600)

# B_cn1 : Main Space Container

        self.B_cn1xMainSpace = QWidget(BxSession)
        self.B_cn1xMainSpace.setObjectName("B_cn1xMainSpace")

        self.gridLayout_MainB = QGridLayout(self.B_cn1xMainSpace)
        self.gridLayout_MainB.setObjectName("gridLayout_MainB")

        BxSession.setCentralWidget(self.B_cn1xMainSpace)

# B_cn1_cn1 : Tab Window Space Container

        self.B_cn1_cn1xTabWindowSpace = QTabWidget(self.B_cn1xMainSpace)
        self.B_cn1_cn1xTabWindowSpace.setObjectName("B_cn1_cn1xTabWindowSpace")

        self.gridLayout_MainB.addWidget(self.B_cn1_cn1xTabWindowSpace)

# B_cn1_cn1_tb1 : Account Tab

        self.B_cn1_cn1_tb1xAccount = QWidget(self.B_cn1_cn1xTabWindowSpace)
        self.B_cn1_cn1_tb1xAccount.setObjectName("B_cn1_cn1_tb1xAccount")

        self.B_cn1_cn1xTabWindowSpace.addTab(self.B_cn1_cn1_tb1xAccount, "Account")

        self.gridLayout_AccountTab = QGridLayout(self.B_cn1_cn1_tb1xAccount)
        self.gridLayout_AccountTab.setObjectName("gridLayout_AccountTab")



        self.gridLayout_AccountTab.addWidget(self.accountTabImport)



# B_cn1_cn1_tb2 : Session Tab

        self.B_cn1_cn1_tb2xSession1 = QWidget(self.B_cn1_cn1xTabWindowSpace)
        self.B_cn1_cn1_tb2xSession1.setObjectName("B_cn1_cn1_tb2xSession1")

        self.B_cn1_cn1xTabWindowSpace.addTab(self.B_cn1_cn1_tb2xSession1, "Session")

### rest of code left out

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
BxSession = QMainWindow()
ui = Ui_BxSession()
ui.configurePageB(BxSession)
BxSession.show()
sys.exit(app.exec_())

我导入了文件,为选项卡创建了一个网格,并尝试将Wiki添加到网格中。但我得到以下错误。

C:\Python27\python.exe "C:/Users/smiths/Desktop/App      Project/AppDev_2/rewritecode/program/session_window.py"
Traceback (most recent call last):
File "C:/Users/smiths/Desktop/App  Project/AppDev_2/rewritecode/program/session_window.py", line 139, in <module>
ui.configurePageB(BxSession)
File "C:/Users/smiths/Desktop/App  Project/AppDev_2/rewritecode/program/session_window.py", line 50, in configurePageB
self.gridLayout_AccountTab.addWidget(self.accountTabImport)
TypeError: arguments did not match any overloaded call:
QGridLayout.addWidget(QWidget): argument 1 has unexpected type 'type'
QGridLayout.addWidget(QWidget, int, int, Qt.Alignment alignment=0): argument  1 has unexpected type 'type'
QGridLayout.addWidget(QWidget, int, int, int, int, Qt.Alignment alignment=0): argument 1 has unexpected type 'type'

Process finished with exit code 1

我也试过

self.accountTabImport = acount1.Ui_MainWindow()

具有相同的错误消息。

我也试图跳过上面的定义,只是包含

self.gridLayout_AccountTab.addWidget(acount1.Ui_MainWindow).

同样的错误。

acount1.py包含一个包含许多函数的类。不要在这里看到需要包含它,但如果需要,请告诉我。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

You need to instantiate the class first (create an object/instance from the class)

self.accountTabImport = acount1.Ui_MainWindow()

(note the brackets at the end)

Note that you may also need to provide arguments to the above call, depening on what is in the method signature of Ui_MainWindow.__init__