如何在wxPython中自动调整* wx.CheckListBox *的大小?

时间:2016-11-22 12:17:35

标签: python wxpython

如果复选框的标签文本大于 wx.CheckListBox 的宽度,则必须更改其大小。

1 个答案:

答案 0 :(得分: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()