gpt4 book ai didi

python - wxPython列表Ctrl : Add Column and Images

转载 作者:行者123 更新时间:2023-11-30 23:13:22 25 4
gpt4 key购买 nike

我在尝试创建专栏并将图像添加到“浏览器列表”时遇到错误。命令行中的错误显示“无法在非报告模式下添加列”。

如何简单地将这个图标及其相应的名称(即“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))

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

最佳答案

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

EDIT2:添加完整示例(修改后的 wxPython 演示 ListCtrl 示例)

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.DataViewListCtrl ( wxPython >= 2.9 ) 是最先进的内置函数,并且还可以将图标添加到列表中。

不包含在此列表中(因为我没有这方面的经验):ObjectListView .

关于python - wxPython列表Ctrl : Add Column and Images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29363743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com