メニューバーからファイルを選択
”FileというメニューバーからOpenを選択すると、ファイルの選択画面がでる”という操作を行うためのGUIを作成することにした。wxPythonのdemoの中では、"Menu"と"FileDialog"が参考になりそうだ。
- Menu.py : 基本的にこのまま、いらない部分を削る。
- FileDialog.py : ファイルを選択する部分だけ抜き出す。
Menu.pyを元にして、MenuMod.pyを作成した。
最初、wildcard=wildcardのところでエラーになったが、文字列wildcardを最初のところで定義することで回避した。
写真はMenuMod.pyの実行結果。run.pyと同じディレクトリに置かないと動作しない。
MenuMod.pyの特徴
- Openメニューを選ぶとファイルの選択ができる。
- ファイルを選択してOKを押してもなにも起こらない。
- Closeメニューを選ぶと画面が閉じる。
- run.pyがないと動作しない。
とりあえずは何もわからずに、いじってみたがまだまだ理想のGUI作成までの道のりは長そうだ。
次の目標としては、
- ログ画面にファイルの内容を表示できるようにする。
- ボタンを押してダイアログが開くのではなく、直接ダイアログが開くようにする(つまり、run.byがなくても単体で動作するようにする)。
以下、MenuMod.pyのソース。
#------------------------------------------------------------------- # import wx, os import images wildcard = "Python source (*.py) |*.py|" \ "Compiled Python (*.pyc) |*.pyc|" \ "SPAM files (*.spam)|*.spam|" \ "All files (*.*)|*.*" #------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self, parent, id, log): wx.Frame.__init__(self, parent, id, 'My Menus', size=(400, 200)) self.log = log self.CenterOnScreen() self.CreateStatusBar() self.SetStatusText("This is the statusbar") tc = wx.TextCtrl(self, -1, "This is comments." , style=wx.TE_READONLY|wx.TE_MULTILINE) # Prepare the menu bar menuBar = wx.MenuBar() # 1st menu from left menu1 = wx.Menu() menu1.Append(101, "&Open", "Open a file") menu1.AppendSeparator() menu1.Append(104, "&Close", "Close this frame") # Add menu to the menu bar menuBar.Append(menu1, "&File") self.SetMenuBar(menuBar) # Menu events self.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight) self.Bind(wx.EVT_MENU, self.Menu101, id=101) self.Bind(wx.EVT_MENU, self.CloseWindow, id=104) # Methods def OnMenuHighlight(self, event): # Show how to get menu item info from this event handler id = event.GetMenuId() item = self.GetMenuBar().FindItemById(id) if item: text = item.GetText() help = item.GetHelp() # but in this case just call Skip so the default is done event.Skip() def Menu101(self, event): # self.log.WriteText("CWD: %s\n" % os.getcwd()) dlg = wx.FileDialog( self, message="Choose a file", defaultDir=os.getcwd(), defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: paths = dlg.GetPaths() self.log.WriteText('You selected %d files:' % len(paths)) for path in paths: self.log.WriteText(' %s\n' % path) self.log.WriteText("CWD: %s\n" % os.getcwd()) dlg.Destroy() def CloseWindow(self, event): self.Close() #--------------------------------------------------------------------------- class TestPanel(wx.Panel): def __init__(self, parent, log): self.log = log wx.Panel.__init__(self, parent, -1) b = wx.Button(self, -1, "Show the Menu sample", (50,50)) self.Bind(wx.EVT_BUTTON, self.OnButton, b) def OnButton(self, evt): win = MyFrame(self, -1, self.log) win.Show(True) #--------------------------------------------------------------------------- def runTest(frame, nb, log): win = TestPanel(nb, log) return win #------------------------------------------------------------------- if __name__ == '__main__': import sys,os import run run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])