非常にシンプルな 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()