将 Wx Frame class 作为变量传递给另一个 Class

Pass Wx Frame class as a variable to another Class

我正在尝试将 WX 帧 class 传递给另一个 class。我有三个py文件如下:

gui_20220510.py - 这包含 gui 代码

import wx

class Frame_Demo(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Frame_Demo.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))

        self.panel_1 = wx.Panel(self, wx.ID_ANY)

        sizer_1 = wx.BoxSizer(wx.VERTICAL)

        self.notebook_1 = wx.Notebook(self.panel_1, wx.ID_ANY)
        sizer_1.Add(self.notebook_1, 1, wx.EXPAND, 0)

        self.notebook_1_pane_1 = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.notebook_1.AddPage(self.notebook_1_pane_1, "notebook_1_pane_1")

        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)

        label_1 = wx.StaticText(self.notebook_1_pane_1, wx.ID_ANY, "This is Test: ")
        sizer_2.Add(label_1, 0, 0, 0)

        self.text_ctrl_1 = wx.TextCtrl(self.notebook_1_pane_1, wx.ID_ANY, "Type")
        sizer_2.Add(self.text_ctrl_1, 0, 0, 0)

        self.notebook_1_pane_2 = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.notebook_1.AddPage(self.notebook_1_pane_2, "notebook_1_pane_2")

        sizer_3 = wx.BoxSizer(wx.VERTICAL)

        sizer_3.Add((0, 0), 0, 0, 0)

        self.notebook_1_pane_2.SetSizer(sizer_3)

        self.notebook_1_pane_1.SetSizer(sizer_2)

        self.panel_1.SetSizer(sizer_1)

        self.Layout()

demo.py - 这包含 运行 函数以导入 wx 框架

from importlib import import_module
from wx import App

global Frame_Demo
global builddate

class Demo_Main(Frame_Demo):
    def __init__(self, *args, **kwds):
        Frame_Demo.__init__(self, *args, **kwds)
        # Update the Frame Title to Reflect Build Date
        self.SetTitle('Demo Tool v' + builddate)
        self.Maximize(True)

def main_run(bdate, gui_version):
    global builddate
    global Frame_Demo
    builddate = bdate
    gui_module = import_module('gui_' + gui_version)
    Frame_Demo = gui_module.Frame_Demo
    app = App()
    frame = Demo_Main(None)
    frame.Show()
    app.MainLoop()

Main.py - 这是 运行 程序

的主要文件
from demo import main_run

builddate = '2022-MAY-10'
gui_version = '20220510'

if __name__ == '__main__':
    main_run(builddate, gui_version)

尝试 运行 main.py 时,出现“Frame_Demo”未定义错误,即使它被定义为全局。

我在这里做错了什么?非常感谢任何指导。提前致谢。

Word global 不适用于创建全局变量。在函数外创建的所有变量都是自动全局的(在当前模块内),但您必须为变量赋值才能创建它。

我们在函数内部使用 global 来通知函数,当我们使用 = 为变量赋新值时,它必须使用 external/global 变量而不是创建本地变量。

所以你必须在定义class Demo_Main(Frame_Demo):之前导入Frame_Demo。或者你必须在 main_run 中定义这个 class,在 Frame_Demo = gui_module.Frame_Demo

之后
from importlib import import_module
from wx import App

def main_run(builddate, gui_version):

    gui_module = import_module('gui_' + gui_version)
    Frame_Demo = gui_module.Frame_Demo

    class Demo_Main(Frame_Demo):
        def __init__(self, *args, **kwds):
            super().__init__(*args, **kwds)
            # Update the Frame Title to Reflect Build Date
            self.SetTitle('Demo Tool v' + builddate)
            self.Maximize(True)

    app = App()
    frame = Demo_Main(None)
    frame.Show()
    app.MainLoop()

编辑:

您也可以将这些值放在单独的文件中 - 即。 settings.py

builddate = '2022-MAY-10'
gui_version = '20220511'

并将其导入到 demo.py

from importlib import import_module
from wx import App

import settings

gui_module = import_module('gui_' + settings.gui_version)
Frame_Demo = gui_module.Frame_Demo

class Demo_Main(Frame_Demo):
    def __init__(self, *args, **kwds):
        super().__init__(*args, **kwds)
        # Update the Frame Title to Reflect Build Date
        self.SetTitle('Demo Tool v' + settings.builddate)
        self.Maximize(True)

def main_run():
    app = App()
    frame = Demo_Main(None)
    frame.Show()
    app.MainLoop()

和运行main.py没有这些值

from demo import main_run

if __name__ == '__main__':
    main_run()