2013-05-01 14 views
-1

我的代碼由幾個類組成 - 有些是UI小部件,有些是純功能的,一個是存儲一些全局設置我從其他Widget訪問的MainWindow)。將我的代碼分成一個文件,每個類 - 類無法看到一個「全局類」

我與Mainwindow一起創建「全局設置類」,其他所有內容均由MainWindow或其子項創建。 - 只要整個代碼位於同一個文件中,它就會工作。

爲了避免愚蠢的滾動我把代碼分成每個類一個文件。使用MainWindow創建「全局設置類」文件。

但現在主窗口的兒童不能訪問/看到「全局設置」級了...

的文件都在同一個文件夾中,我試圖import xxxfrom xxx import *import xxx as x

通過評論所有「選項」相關代碼(並失去此功能),它似乎工作。

我只是不明白爲什麼它分裂了整個事情後不應該工作。

編輯 - 細節:

文件1:

class MainWindow(QMainWindow): 
    def __init__(self): 
     QMainWindow.__init__(self) 

     ... # A MenuBar with checkable Items, QAction calls a function which sets the option in o_option instance 

    def startWidgetXYZ(self): 
     self.setWindowTitle(...) 
     self.initAllChecks() 
     xyz = XYZ(file, topic, self) 
     self.setCentralWidget(xyz) 

o_options = MenuBarOptions() # this is the global 
app = QApplication(sys.argv) 
m_window = MainWindow() 
m_window.show() 
sys.exit(app.exec_()) 

文件2:

class XYZ(QWidget): 
    def __init__(self, file, topic, parent=None): 
     QWidget.__init__(self, parent) 

     self.k_korpus = Korpus(file, topic) # the class Korpus will load/save data from/to XML-files 

文件3:

class Korpus(object): 
    def __init__(self, file, topic): 

     self.file = file 
     self.level = level 
     self.patienceChecked = o_options.isOption123Checked() # NameError: global name 'o_options' is not defined - (but no error if code is in one file) 
+1

沒有代碼和回溯,這實質上是無法回答的。我們可以給你一些含糊的聲明,說明你需要如何確保全局對象在你需要的地方被導入,但是沒有看到那些導入是如何失敗的,我們真的會在黑暗中捅死。 – 2013-05-01 10:00:40

+0

啊謝謝。我認爲我的整個想法有一些內在的錯誤。 – Chrugel 2013-05-01 10:01:57

回答

0

PUT 「全局設置類」 不同文件爲MainWindow。然後在每個文件(主文件1和文件2)中添加如

from global_settings_file import GlobalSettingsClass 
+0

該類的代碼本身已經存在於它自己的文件中。在您的評論之後,我將MainWindow的實例化移走,並將其與代碼本身放在一起......它可以工作。謝謝! – Chrugel 2013-05-01 11:05:44

相關問題