link 按钮打开另一个 py 文件的正确方法是什么?

What is the correct way to link a button to open an other py file?

我很怀疑,因为我的脚本没有按预期执行。所以我有一个简单的主文件,带有一个打开空白网格的按钮(下面的代码)。此代码的问题是它在第一次执行时打开 reportWindow,但如果我关闭报告并尝试再次打开它,我会收到错误消息: NameError:名称 'TestFrame' 未定义

我还从 reportWindow.py 的最后几行中删除了 if __name__ == '__main__',因为脚本无法使用它。我尝试了 if __name__ == 'main',因为它是从 main.py 导入的,但它也没有用。

拜托,有人可以告诉我应该如何以正确的方式完成吗?

谢谢

main.py

import wx

class Test(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, "Frame aka Window", size=(300, 200))
        panel = wx.Panel(self)
        button = wx.Button(panel, label = "Exit", pos=(80, 80), size = (120,30))
        self.Bind(wx.EVT_BUTTON, self.closebutton, button)

    def closebutton(self,event):
        from reportWindow import SimpleGrid
        SimpleGrid(TestFrame, -1)

if __name__ == '__main__':
    app = wx.App()  
    frame = Test(parent=None, id=1)
    frame.Show()
    app.MainLoop()

reportWindow.py

import wx
import wx.grid as gridlib

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)

        #[...Some code...]


class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, 0, "Title", size=(1400,800))
        self.grid = SimpleGrid(self, log)

        #[...Some code...]


#if __name__ == '__main__':
import sys
from wx.lib.mixins.inspection import InspectableApp
app = InspectableApp(False)
frame = TestFrame(None, sys.stdout)
frame.Show(True)
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
app.MainLoop()

您的代码有一些问题。

1) 出现 NameError 是因为您没有从 reportWindow.

导入 TestFrame

2) 如果 reportWindow.py 中没有 if __name__ == "__main__",您的程序将创建另一个 wx.App 并启动另一个 MainLoop,这将阻塞接收事件等的第一个循环......你应该只创建 1 App/MainLoop。它还会在您第一次导入 reportWindow 时创建 TestFrame

3) 看起来您希望 SimpleGrid 成为 TestFrame 的 child,但随后您尝试创建它本身在 closebutton

4) 创建 SimpleGrid 时,您传递 TestFrame class 而不是 TestFrame class.

这里是修改后的代码

# main.py
import wx, sys


class Test(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "Frame aka Window", size=(300, 200))
        panel = wx.Panel(self)
        button = wx.Button(panel, label="Exit", pos=(80, 80), size=(120, 30))
        self.Bind(wx.EVT_BUTTON, self.closebutton, button)

    def closebutton(self, event):
        from reportWindow import TestFrame

        frame = TestFrame(self, sys.stdout)
        frame.Show()


if __name__ == '__main__':
    app = wx.App()
    frame = Test(parent=None, id=1)
    frame.Show()
    app.MainLoop()

# reportWindow.py
import wx
import wx.grid as gridlib


class SimpleGrid(gridlib.Grid):  ##, mixins.GridAutoEditMixin):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)

        # [...Some code...]


class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, 0, "Title", size=(1400, 800))
        self.grid = SimpleGrid(self, log)

        # [...Some code...]


if __name__ == '__main__':
    import sys
    from wx.lib.mixins.inspection import InspectableApp

    app = InspectableApp(False)
    frame = TestFrame(None, sys.stdout)
    frame.Show(True)
    import wx.lib.inspection
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()