将我的代码分成每个类的一个文件后 - 类无法看到一个“全局类”

时间:2013-05-01 09:58:34

标签: python python-3.x pyqt4

我的代码由几个类组成 - 一些是UI Widgets,一些是纯函数,一个是存储一些全局设置(来自MainWindow的设置,我从其他Widgets访问)。

我与Mainwindow一起创建了“global settings-class”,其他一切都是由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_())

file2的:

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

file3的:

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 个答案:

答案 0 :(得分:0)

将“global settings-class”放在另一个文件中MainWindow。然后在每个文件(主文件1和文件2)中添加类似

的内容
from global_settings_file import GlobalSettingsClass