我在尝试创建列并将图像添加到" browserlist"时收到错误。命令行中的错误表示"无法在非报告模式下添加列"。
如何简单地添加此图标及其相应的名称,即" Google Chrome"到列表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))
我希望它看起来有点像下面窗口的左侧:
答案 0 :(得分:2)
在wxPython演示中查找ListCtrl
示例(如果没有,请立即安装 )。它在行文本前面添加了图标。为了能够添加列,您必须将样式设置为 wx.LC_REPORT
(编辑) (在该模式下,您将限制为16x16图标) (EDIT3,不是真的)。
EDIT2 :添加完整示例(修改了wxPython演示ListCtrl示例)
EDIT4 :修改示例,删除列表解包。
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.DataViewListCtrl
(wxPython
> = 2.9)是内置版本中最先进的,也可以将图标添加到列表中。
未包含在此列表中(因为我没有相关经验):ObjectListView
。
答案 1 :(得分:0)
"无法在非报告模式下添加列"给你提示。 见:http://wxpython.org/Phoenix/docs/html/ListCtrl.html?highlight=listctrl#styles-window-styles
所以改变:
browserlist=wx.ListCtrl(panel, pos=(255, 100), size=(220, 100))
为:
browserlist=wx.ListCtrl(panel, pos=(255, 100), size=(220, 100), style=wx.LC_REPORT)