如何允许 UltimateListCtrl 重新排列自身,wxpython

发布于 2024-12-24 17:18:15 字数 1256 浏览 1 评论 0原文

如何允许 UltimateListCtrl 能够根据用户输入重新排序/重新排列自身?以下显示了该功能,但我不确定如何做到这一点。 数据 = { 0:(“3”,“3”,“1”), 1:(“2”,“1”,“2”), 2:(“1”、“2”、“3”) }

import wx
import wx.lib.mixins.listctrl as listmix
from wx.lib.agw import ultimatelistctrl as ULC

class MyList(ULC.UltimateListCtrl, listmix.ColumnSorterMixin):
    def __init__(self, parent, columns):
        ULC.UltimateListCtrl.__init__(self, parent, agwStyle=ULC.ULC_REPORT | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
        self.itemDataMap = DATA
        listmix.ColumnSorterMixin.__init__(self, columns)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumn)

    def OnColumn(self, e):
        self.Refresh()
        e.Skip()

    def GetListCtrl(self):
        return self

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.list = MyList(self, 3)
        self.list.InsertColumn(0, "A")
        self.list.InsertColumn(1, "B")
        self.list.InsertColumn(2, "C")

        items = DATA.items()
        for key, data in items:
            index = self.list.Append(data)
            self.list.SetItemData(index, key)

        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

how can I allow a UltimateListCtrl, to be able to reorder/rearrange itself with the users input? The following shows the ability however i am not sure how it can be done.
DATA = {
0 : ("3", "3", "1"),
1 : ("2", "1", "2"),
2 : ("1", "2", "3")
}

import wx
import wx.lib.mixins.listctrl as listmix
from wx.lib.agw import ultimatelistctrl as ULC

class MyList(ULC.UltimateListCtrl, listmix.ColumnSorterMixin):
    def __init__(self, parent, columns):
        ULC.UltimateListCtrl.__init__(self, parent, agwStyle=ULC.ULC_REPORT | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
        self.itemDataMap = DATA
        listmix.ColumnSorterMixin.__init__(self, columns)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumn)

    def OnColumn(self, e):
        self.Refresh()
        e.Skip()

    def GetListCtrl(self):
        return self

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.list = MyList(self, 3)
        self.list.InsertColumn(0, "A")
        self.list.InsertColumn(1, "B")
        self.list.InsertColumn(2, "C")

        items = DATA.items()
        for key, data in items:
            index = self.list.Append(data)
            self.list.SetItemData(index, key)

        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2024-12-31 17:18:15

也许您需要这样的东西:

class MyList(ULC.UltimateListCtrl, listmix.ColumnSorterMixin):
    def __init__(self, parent, data):
        ULC.UltimateListCtrl.__init__(self, parent, agwStyle=ULC.ULC_REPORT | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
        self.data = data
        self.IDoMyself()
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumn)

    def IDoMyself(self):
        self.itemDataMap = self.data
        listmix.ColumnSorterMixin.__init__(self, len(self.data))

        self.InsertColumn(0, "A")      # you should generalize this for any size
        self.InsertColumn(1, "B")
        self.InsertColumn(2, "C")

        for key, data in self.data.items():
            index = self.Append(data)
            self.SetItemData(index, key)

    def OnColumn(self, e):
        self.Refresh()
        e.Skip()

    def GetListCtrl(self):
        return self

请注意,您还应该自动执行 InsertColumn 过程,以通过编程方式考虑列数。如果您不需要特殊的列名并且只使用几个列名,则可以使用 string.ascii_uppercase 来获取每列的字母。

然后在框架中实例化该类:

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        DATA = { 0 : ("3", "3", "1"), 1 : ("2", "1", "2"), 2 : ("1", "2", "3") }
        self.list = MyList(self, DATA)
        self.Show()

我将 DATA 放在这里,但稍微修改一下代码,它可以通过像 MainWindow(self, DATA) 这样的调用从外部发送。

Maybe you are asking for something like this:

class MyList(ULC.UltimateListCtrl, listmix.ColumnSorterMixin):
    def __init__(self, parent, data):
        ULC.UltimateListCtrl.__init__(self, parent, agwStyle=ULC.ULC_REPORT | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
        self.data = data
        self.IDoMyself()
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumn)

    def IDoMyself(self):
        self.itemDataMap = self.data
        listmix.ColumnSorterMixin.__init__(self, len(self.data))

        self.InsertColumn(0, "A")      # you should generalize this for any size
        self.InsertColumn(1, "B")
        self.InsertColumn(2, "C")

        for key, data in self.data.items():
            index = self.Append(data)
            self.SetItemData(index, key)

    def OnColumn(self, e):
        self.Refresh()
        e.Skip()

    def GetListCtrl(self):
        return self

Note you should also automate the InsertColumn process to take into account the number of columns programmatically. If you dont need special column names and you are only to use a few, you could use string.ascii_uppercase to get the letter for each column.

Then you instantiate the class in your Frame:

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        DATA = { 0 : ("3", "3", "1"), 1 : ("2", "1", "2"), 2 : ("1", "2", "3") }
        self.list = MyList(self, DATA)
        self.Show()

I put DATA here but modifying the code a bit it could be send from outside with a call like MainWindow(self, DATA).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文