链接按钮以打开其他py文件的正确方法是什么?

时间:2019-05-26 04:26:42

标签: python wxpython

我有一个很大的疑问,因为我的脚本无法正常运行。因此,我有一个带有按钮的简单主文件,该文件可打开空白网格(下面的代码)。此代码的问题是它在第一次执行时打开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 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。

1)发生NameError是因为您没有从TestFrame导入reportWindow

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

3)似乎您希望SimpleGridTestFrame的子级,但是随后您尝试在closebutton中自己创建它。

4):创建SimpleGrid时,您传递的是TestFrame类,而不是TestFrame类的实例。

这是修改后的代码

# 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()