python wxpython全局名称未定义

时间:2013-07-20 12:37:46

标签: python wxpython

我正在尝试开发一个非常简单的wxpython GUI。目前只有一个打开文件对话框的按钮&在那个文本控制框下面。目前我所要做的就是将打开的文件名打印到文本控制框,但不断收到错误消息。 “未定义全球名称”。任何帮助将不胜感激!

#!/usr/bin/python

import os
import wx
import wx.lib.agw.multidirdialog as MDD

wildcard = "Python source (*.py)|*.py|" \
            "All files (*.*)|*.*"

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

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY)
        panel = wx.Panel(self, wx.ID_ANY)
        self.currentDirectory = os.getcwd()

        text = wx.TextCtrl(panel, -1, "",style=wx.TE_MULTILINE|wx.HSCROLL)

        # create the buttons and bindings
        openFileDlgBtn = wx.Button(panel, label="Show OPEN FileDialog")
        openFileDlgBtn.Bind(wx.EVT_BUTTON, self.onOpenFile)

        # put the buttons in a sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(openFileDlgBtn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onOpenFile(self, event):
        """
        Create and show the Open FileDialog
        """
        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultDir=self.currentDirectory, 
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
            )
        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPaths()
            print "You chose the following file(s):"
            for path in paths:
                print path
                text.AppendText('path') 
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:2)

这是一个有根据的猜测,完全基于眼睛的代码:

您在text方法中引用了onOpenFile(),但未在该方法中定义它。 text是另一种方法中的本地名称。

如果您希望通过TextCtrl方法访问分配给text的{​​{1}}对象,请在__init__上存储对其的引用,以便您可以参考其他方法也是:

self