2016-11-12 77 views
0

我正在用Python 2.7創建一個程序,並且遇到了幾小時搜索Google和論壇未回答的問題。Python 2.7 PyQt4將主窗口文件添加到另一個主窗口的選項卡

我的程序由一個名爲sessions_window.py的Main_Window組成,它包含一個Tab Widget。在每個選項卡中,應該包含一個單獨的主窗口文件。例如,sessions_window.py的一個選項卡是帳戶選項卡,並且我有另一個包含帳戶信息的acount1.py文件,我想將它嵌入帳戶選項卡(並且是的,我知道我拼寫帳戶「acount」,I稍後會修復)。另一個選項卡將被稱爲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_()) 

我已導入的文件,創造了標籤的網格,並試圖addWidget電網。但我得到下面的錯誤。

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包含一個包含一堆函數的類。不要看到需要在這裏包括它,但如果需要的話讓我知道。任何幫助將不勝感激。

回答

0

您需要先實例化類(創建一個對象從類/實例)

self.accountTabImport = acount1.Ui_MainWindow() 

(注意最後括號),您可能還需要提供參數

註上述呼籲,取決於方法簽名中的內容Ui_MainWindow.__init__