我花了一些时间才弄清楚这一点。我认为您实际上是通过指定位置在框架上绘制单选按钮!这显然在这种情况下不起作用,因为当您尝试选择单选按钮时,您的 listCtrl 会出现在前台。
如果您发现单选按钮未根据列表项正确对齐。按钮之间的间距大于列表行之间的间距。
使用 list_ctrl 的问题是您不能在其中放置单选按钮等小部件。
我建议您为此使用 wx.ScrolledWindow 之类的东西。将每一行放入一个 sizer 并垂直堆叠这些 sizer。
更新:
import wx
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
sizer0 = wx.BoxSizer( wx.VERTICAL )
self.scrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.scrolledWindow.SetScrollRate( 5, 5 )
grid_sizer = wx.GridSizer( 0, 8, 0, 0 )
self.head1 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Code", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head1.Wrap( -1 )
grid_sizer.Add( self.head1, 0, wx.ALL, 5 )
self.head2 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Classification", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head2.Wrap( -1 )
grid_sizer.Add( self.head2, 0, wx.ALL, 5 )
self.head3 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"A", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head3.Wrap( -1 )
grid_sizer.Add( self.head3, 0, wx.ALL, 5 )
self.head4 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"B", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head4.Wrap( -1 )
grid_sizer.Add( self.head4, 0, wx.ALL, 5 )
self.head5 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"C", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head5.Wrap( -1 )
grid_sizer.Add( self.head5, 0, wx.ALL, 5 )
self.head6 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"D", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head6.Wrap( -1 )
grid_sizer.Add( self.head6, 0, wx.ALL, 5 )
self.head7 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Cond.", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head7.Wrap( -1 )
grid_sizer.Add( self.head7, 0, wx.ALL, 5 )
self.head8 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Update", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head8.Wrap( -1 )
grid_sizer.Add( self.head8, 0, wx.ALL, 5 )
self.column11 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column11.Wrap( -1 )
grid_sizer.Add( self.column11, 0, wx.ALL, 5 )
self.column12 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Urban", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column12.Wrap( -1 )
grid_sizer.Add( self.column12, 0, wx.ALL, 5 )
self.column13 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"68", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column13.Wrap( -1 )
grid_sizer.Add( self.column13, 0, wx.ALL, 5 )
self.column14 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"80", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column14.Wrap( -1 )
grid_sizer.Add( self.column14, 0, wx.ALL, 5 )
self.column15 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column15.Wrap( -1 )
grid_sizer.Add( self.column15, 0, wx.ALL, 5 )
self.column16 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"89", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column16.Wrap( -1 )
grid_sizer.Add( self.column16, 0, wx.ALL, 5 )
self.column17 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.column17.Wrap( -1 )
grid_sizer.Add( self.column17, 0, wx.ALL, 5 )
radio1Choices = [ u"G", u"P", u"F" ]
self.radio1 = wx.RadioBox( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, radio1Choices, 3, wx.RA_SPECIFY_COLS )
self.radio1.SetSelection( 0 )
grid_sizer.Add( self.radio1, 0, wx.ALL, 5 )
self.scrolledWindow.SetSizer( grid_sizer )
self.scrolledWindow.Layout()
grid_sizer.Fit( self.scrolledWindow )
sizer0.Add( self.scrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )
# Bind the radio box select event to a function
self.radio1.Bind( wx.EVT_RADIOBOX, self.on_selected )
self.SetSizer( sizer0 )
self.Layout()
self.Maximize()
self.Centre( wx.BOTH )
self.Show()
def on_selected(self, event):
# Depending upon the option selected the values of A,B,C,D are changed
if self.radio1.GetStringSelection() == 'P':
self.column13.SetLabel('25')
self.column14.SetLabel('27')
self.column15.SetLabel('34')
self.column16.SetLabel('12')
elif self.radio1.GetStringSelection() == 'F':
self.column13.SetLabel('56')
self.column14.SetLabel('70')
self.column15.SetLabel('49')
self.column16.SetLabel('54')
else:
self.column13.SetLabel('78')
self.column14.SetLabel('83')
self.column15.SetLabel('69')
self.column16.SetLabel('100')
if __name__ == "__main__":
app = wx.App()
Frame(None)
app.MainLoop()
我编写了这个示例代码来向您展示如何做到这一点。这有点粗略,但我希望你能明白。
您可能还想查看此帖子中的答案:Adding a button to every row in ListCtrl WxPython。这可能是一个更好的选择。