可扩展的wxpython listctrl列

时间:2013-04-26 22:43:43

标签: python wxpython

我正在尝试使用wxlistctrl创建一个表,其中列将与其父级一起增长。我希望它的功能就像一个带有可增长列的网格包装器。我也不希望用户通过单击并拖动垂直规则来调整宽度。这是可能的listctrl或我将不得不自己控制?

这是我现在拥有的一个例子

import wx
class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0
        self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         )
        self.list_ctrl.InsertColumn(0, 'Subject')
        self.list_ctrl.InsertColumn(1, 'Due')
        self.list_ctrl.InsertColumn(2, 'Location', width=125)

        btn = wx.Button(panel, label="Add Line")
        btn.Bind(wx.EVT_BUTTON, self.add_line)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    def add_line(self, event):
        line = "Line %s" % self.index
        self.list_ctrl.InsertStringItem(self.index, line)
        self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
        self.list_ctrl.SetStringItem(self.index, 2, "USA")
        self.index += 1

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

3 个答案:

答案 0 :(得分:1)

请参阅wx.lib.mixins.listctrl模块中的ListCtrlAutoWidthMixin类。

答案 1 :(得分:0)

我不确定这是否是你想要的,但你可以试试这个;

sizer.Add(self.list_ctrl, 1, wx.ALL | wx.GROW, 5)

Demo

遗憾的是,您无法禁用列宽调整大小。至少不在默认库中; http://wxpython.org/Phoenix/docs/html/ListCtrl.html

答案 2 :(得分:0)

  

我也不希望用户通过点击并拖动垂直规则来调整宽度。

这至少是可能的:

    self.list_ctrl.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColumnResizeStart)
# ...
def OnColumnResizeStart(self, event):
    # Veto if on auto mode:
    if self.autoColWidths: event.Veto()
    else: event.Skip()