wxPython:使变量可用于导入的类?

2024-06-28 19:10:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个wxpythongui,它在单击按钮时分配一些变量。你知道吗

def OnGo(self, event):
    inputdatadirectory = self.txtbx1.GetValue() 
    outputsavedirectory = self.txtbx2.GetValue()
    mcadata = self.txtbx3.GetValue()
    current_dir = os.getcwd()
    execfile(current_dir+"\\aEDXD.py")

然后我运行execfile,执行的文件从另一个文件导入并运行一个类。你知道吗

如何使通过GUI定义的变量对导入的类可用?你知道吗


Tags: 文件selfeventdefdircurrent按钮execfile
1条回答
网友
1楼 · 发布于 2024-06-28 19:10:21

是的,你可以,虽然它可能很难调试。下面是一个愚蠢的例子:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #                                   
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = wx.Panel(self)
        btn = wx.Button(panel, label="Go")
        btn.Bind(wx.EVT_BUTTON, self.onGo)
        self.Show()

    #                                   
    def onGo(self, event):
        """"""
        foobar = "This is a test!"
        execfile("foo.py")

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

下面是foo.py公司文件。你知道吗

print foobar

如果运行wxPython脚本,它将执行foo.py公司它可以访问wxPython脚本中的局部变量和全局变量。你不能跑了foo.py公司但它本身。请参见以下内容:

相关问题 更多 >