怪奇wxGrid
wx.Gridでハマった。
以下のソースは、ぱっと見だと問題ないように見える。
testWxGrid.py
# -*- coding: utf-8 -*- import wx from wx import xrc class testWxGrid(wx.Dialog): def __init__(self, parent): self.res= xrc.XmlResource( "testWxGrid.xrc" ) dlg= self.res.LoadDialog(None, 'testWxGrid') self.PostCreate(dlg) self._init_Grid() def _init_Grid(self): self.grid = xrc.XRCCTRL(self, "testGrid") # Grid self.grid.CreateGrid( 3, 4 ) #col self.grid.AutoSizeColumns() self.grid.SetColLabelValue( 0, u"NHK-G" ) self.grid.SetColLabelValue( 1, u"NHK-E" ) self.grid.SetColLabelValue( 2, u"TVK" ) self.grid.SetColLabelValue( 3, u"TVQ" ) # Rows self.grid.AutoSizeRows() self.grid.EnableDragRowSize( False ) self.grid.Fit() if __name__ == '__main__': app = wx.PySimpleApp(0) dlg = testWxGrid(None) dlg.ShowModal() dlg.Destroy()
(xrcは省略。ちなみにwxFormBuilderで作成したやつ)
ところがこれが動かない。以下のエラーを吐く。
Traceback (most recent call last): File "testWxGrid.py", line 30, in <module> dlg = testWxGrid(None) File "testWxGrid.py", line 11, in __init__ self._init_Grid() File "testWxGrid.py", line 16, in _init_Grid self.grid.CreateGrid( 3, 4 ) AttributeError: 'ScrolledWindow' object has no attribute 'CreateGrid'
wxGridを継承しているはずのself.gridが、なぜかwx.gridの継承元の一つであるwxScrolledWindowの派生クラスと認識されている。
謎。
wxFormBuilderで、デザインファイルのpython出力を見てみると、こんな感じになってる。
# -*- coding: utf-8 -*- ######################################################### ## Python code generated with wxFormBuilder (version Sep 8 2010) ## http://www.wxformbuilder.org/ ## ## PLEASE DO "NOT" EDIT THIS FILE! ######################################################### import wx import wx.grid ######################################################### ## Class testWxGrid ######################################################### class testWxGrid ( wx.Dialog ): def __init__( self, parent ): wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"testWxGrid", pos = wx.DefaultPosition, size = wx.Size( 515,185 ), style = wx.DEFAULT_DIALOG_STYLE ) 〜〜〜以下略〜〜〜
wx.gridのインポート文が怪しい。
testWxGrid.pyに加えてみると、たしかに動く。
なんだこれ。