wxPython List Ctrl:添加列和图像

2024-05-19 11:04:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个错误,试图使列和添加图像到“浏览列表”。命令行中的错误是“无法在非报表模式下添加列”。在

我怎样才能简单地将这些图标及其对应的名称,即“googlechrome”添加到列表ctrl中?在

images=['/Desktop/chromelogo.png', 'Desktop/firefoxlogo.png']
browserlist=wx.ListCtrl(panel, pos=(255, 100), size=(220, 100))
browserlist.InsertColumn(0, '')

self.il = wx.ImageList(40,40,True)
for i in images:
    self.il.Add(wx.Bitmap(i))

我希望它看起来像下面窗户的左边: enter image description here


Tags: 命令行图像self名称列表报表png错误
2条回答

不能在“非”列中给出“不能添加”的提示。 参见:http://wxpython.org/Phoenix/docs/html/ListCtrl.html?highlight=listctrl#styles-window-styles

所以改变一下:

browserlist=wx.ListCtrl(panel, pos=(255, 100), size=(220, 100))

收件人:

^{pr2}$

在wxPython演示中查找ListCtrl示例(如果没有,请立即安装)。它在行文本前面有图标。要想添加列,您必须将样式设置为wx.LC_REPORT(编辑)(在该模式下,您将被限制为16x16个图标)(EDIT3,不正确)。在

EDIT2:添加完整示例(修改的wxPython demo listcrl示例)

EDIT4:示例修改,删除列表解包。在

with screenshot

import  wx

test_list_data = {
1 : ("New", "Explanation text new"),
2 : ("Open", "Explanation text open"),
3 : ("Copy", "Explanation text copy"),
4 : ("Paste", "Explanation text paste")}

class TestListCtrlPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        wx.Panel.__init__(self, *args, **kwds)

        sizer = wx.BoxSizer(wx.VERTICAL)
        BMP_SIZE = 24
        tsize = (BMP_SIZE, BMP_SIZE)
        self.il = wx.ImageList(BMP_SIZE, BMP_SIZE)

        # bitmap generation, uses stock bitmaps included in wxPython
        new_bmp =  wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, tsize)
        open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize)
        copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR, tsize)
        paste_bmp= wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR, tsize)

        self.bmpdict = {1: new_bmp, 2: open_bmp, 3: copy_bmp, 4: paste_bmp}

        # mapping wxImageList indices to keys in test_list_data
        self.imglistdict = {}

        for idx, bmp in self.bmpdict.iteritems():
            self.imglistdict[idx] = self.il.Add(bmp)

        self.listctl = wx.ListCtrl(self, -1,
                                 style=wx.LC_REPORT 
                                 #| wx.BORDER_SUNKEN
                                 | wx.BORDER_NONE
                                 | wx.LC_EDIT_LABELS
                                 | wx.LC_SORT_ASCENDING
                                 #| wx.LC_NO_HEADER
                                 #| wx.LC_VRULES
                                 #| wx.LC_HRULES
                                 #| wx.LC_SINGLE_SEL
                                 )

        self.listctl.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
        sizer.Add(self.listctl, 1, wx.EXPAND)

        self.PopulateList()

        self.SetSizer(sizer)
        self.SetAutoLayout(True)

    def PopulateList(self):
        # header creation
        info = wx.ListItem()
        info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
        info.m_image = -1
        info.m_format = 0
        info.m_text = "Artist"
        self.listctl.InsertColumnInfo(0, info)

        info.m_text = "Title"
        self.listctl.InsertColumnInfo(1, info)

        # ListCtrl data generation
        items = test_list_data.items()
        for key, data in items:
            imglist_idx = self.imglistdict[key]
            index = self.listctl.InsertImageStringItem(key, data[0], imglist_idx)
            self.listctl.SetStringItem(index, 1, data[1])
            self.listctl.SetItemData(index, key)

class listctltest(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.pnl = TestListCtrlPanel(self, -1)

if __name__ == '__main__':
    app = wx.App(redirect=False)
    frm = listctltest(None, -1, 'title')
    frm.Show()
    app.MainLoop()

如果有您想要的东西,您也可以在演示中查看UltimateListCtrl。在

wx.DataViewListCtrlwxPython>;=2.9)是最先进的内置函数,还可以向列表中添加图标。在

不包括在这个列表中(因为我没有这方面的经验):ObjectListView。在

相关问题 更多 >

    热门问题