wxPythonことはじめ
wxPythonでFrameを表示する最小プログラム
>>> import wx >>> app = wx.PySimpleApp() >>> frm = wx.Frame(None) >>> frm.Show() True >>> app.MainLoop()
出力のリダイレクト
wx.Appの__init__メソッドで指定する。redirectをTrueにすると出力用のFrameが表示される。redirectをTrueにして、filenameを文字列でファイル名を指定するとファイルに出力される。redirectをFalseにするとコンソールに出力される。リダイレクトしてもwx.Appの初期化前や終了後はコンソールに出力されたり、OnExitはredirectをFalseにしないと'OnExit'の表示が見れなかったりなどは注意。
import wx class App(wx.App): def __init__(self, redirect=True, filename=None): print 'App.__init__' wx.App.__init__(self, redirect, filename) def OnInit(self): print 'OnInit' self.frame = wx.Frame(parent=None, id=-1, title='Title') self.frame.Show() self.SetTopWindow(self.frame) return True def OnExit(self): print 'OnExit' if __name__ == '__main__': app = App(True) app.MainLoop()
wx.Frameのコンストラクタのシグネチャ
wx.Frame(parent, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="frame")
idはFrameなどのwidget(部品)を識別するものでユニークに付ける。-1を指定すると自動的に付けてくれる。
posとsizeは、wx.Point、wx.Sizeにより指定できる。wx.Pointやwx.Sizeを使える全ての場所で代わりに(10, 20)のようなタプルを指定できる。
styleはwidgetのスタイルをビットマスクにより指定する。指定できるスタイルはwidgetごとに異なる。
ダイアログの表示
>>> app = wx.PySimpleApp() >>> dlg = wx.MessageDialog(None, 'This is a message.', 'Title', wx.YES_NO | wx.ICON_QUESTION) >>> result = dlg.ShowModal() >>> dlg.Destroy() True
wx.MessageDialogはwx.ID_YES、wx.ID_NO、wx.ID_CANCEL、wx.ID_OKのいづれかを返す。
スタイルは、wx.OK、wx.CANCEL、wx.YES_NOとwx.ICON_EXCLAMATION、wx.ICON_INFORMATION、wx.ICON_QUESTIONを組み合わせて指定可能。