wxPython List Ctrl:添加列和图像
wxPython List Ctrl: Add Column and Images
我在尝试创建专栏并将图像添加到 "browserlist" 时遇到错误。来自命令行的错误显示 "can't add column in non report mode"。
我怎样才能简单地将这些图标及其相应的名称添加到列表 ctrl 中,即 "Google chrome"?
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))
我希望它看起来有点像下面 window 的左侧:
在 wxPython 演示中查找 ListCtrl
示例(如果没有,请 现在 安装它)。它在行文本前添加了图标。为了能够添加列,您必须将样式设置为 wx.LC_REPORT
(EDIT) (在该模式下您将被限制为 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
.
"can't add column in non report mode" 给你提示。
参见: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)
我在尝试创建专栏并将图像添加到 "browserlist" 时遇到错误。来自命令行的错误显示 "can't add column in non report mode"。
我怎样才能简单地将这些图标及其相应的名称添加到列表 ctrl 中,即 "Google chrome"?
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))
我希望它看起来有点像下面 window 的左侧:
在 wxPython 演示中查找 ListCtrl
示例(如果没有,请 现在 安装它)。它在行文本前添加了图标。为了能够添加列,您必须将样式设置为 wx.LC_REPORT
(EDIT) (在该模式下您将被限制为 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
.
"can't add column in non report mode" 给你提示。 参见: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)