2016-11-22 22 views

回答

2

簡短的回答是不設置CheckListBox的大小。這會自動將尺寸設置爲最大的項目。如果確實設置了尺寸參數且文字不適合,請設置style=wx.LB_HSCROLL,如果需要,將會創建一個水平滑塊。
下面的兩個版本的演示self.clb

import wx 

class ClbView(wx.Frame): 
    def __init__(self, parent): 
     wx.Frame.__init__(self, None, -1, 'CheckListBox', size=(250, 200)) 

    def createWidgets(self, list): 
     self.panel = wx.Panel(self, -1) 
#  self.clb = wx.CheckListBox(self.panel, -1, size=(100,150),choices=list,style=wx.LB_HSCROLL) 
     self.clb = wx.CheckListBox(self.panel, -1, choices = list) 
     self.btn_exit = wx.Button(self.panel, wx.ID_ANY, 'Exit') 

    def sizeWidgets(self): 
     self.vbox = wx.BoxSizer(wx.VERTICAL) 
     self.vbox.Add(self.clb, 0, wx.ALL|wx.CENTER, 5) 
     self.vbox.Add(self.btn_exit, 0, wx.CENTER) 
     self.panel.SetSizer(self.vbox) 
     self.Centre() 

class ClbControl: 
    def __init__(self): 
     self.list = ['Quite a long piece of text', 'Short text', 'X'] 
     self.createView() 

    def createView(self): 
     self.view = ClbView(None) 
     self.view.createWidgets(self.list) 
     self.view.sizeWidgets() 
     self.view.Show() 
     self.view.btn_exit.Bind(wx.EVT_BUTTON, self.onExit) 
     self.view.clb.Bind(wx.EVT_CHECKLISTBOX, self.onCLB) 

    def onCLB(self, evt): 
     x = int(evt.GetSelection()) 
     print "Box ",x," Accessed" 

    def onExit(self, evt): 
     self.view.Close() 

if __name__ == '__main__': 
    app = wx.App() 
    controller = ClbControl() 
    app.MainLoop()