Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wx Python中将图像添加到ListCtrl最右边的列 太长,读不下去了_Python_Wxpython_Wxwidgets - Fatal编程技术网

在wx Python中将图像添加到ListCtrl最右边的列 太长,读不下去了

在wx Python中将图像添加到ListCtrl最右边的列 太长,读不下去了,python,wxpython,wxwidgets,Python,Wxpython,Wxwidgets,在Python中是否可以将图像添加到wx.ListCtrl的第二列/最右边的列?这就是我想要的: 背景 我有一个wx.ListCtrl和图像列表,如下所示: self.health_grid = wx.ListCtrl(self, style=wx.LC_REPORT| wx.LC_ALIGN_TOP) self.health_grid.InsertColumn(0, heading="Organ") self.health_grid.InsertColumn(1, hea

在Python中是否可以将图像添加到wx.ListCtrl的第二列/最右边的列?这就是我想要的:

背景 我有一个wx.ListCtrl和图像列表,如下所示:

self.health_grid = wx.ListCtrl(self, style=wx.LC_REPORT| wx.LC_ALIGN_TOP)
self.health_grid.InsertColumn(0, heading="Organ")
self.health_grid.InsertColumn(1, heading="Status")
self.health_icons = wx.ImageList(16, 16, False, 0)
self.unhealthy = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "unhealthy.png"))
self.health_icons.Add(self.unhealthy)
self.healthy = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "healthy.png"))
self.health_icons.Add(self.healthy)
self.unknown = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "unknown.png"))
self.health_icons.Add(self.unknown)
self.health_grid.SetImageList(self.health_icons, wx.IMAGE_LIST_SMALL)
self.health_grid.Append(["Heart", self.unhealthy])
尝试1 当我以以下方式添加项目时:

self.health_grid.InsertImageStringItem(self.health_grid.GetItemCount(), "Heart", 0)
self.health_grid.InsertImageStringItem(self.health_grid.GetItemCount(), "Brain", 0)
self.health_grid.InsertImageStringItem(self.health_grid.GetItemCount(), "Lungs", 1)
self.health_grid.InsertImageStringItem(self.health_grid.GetItemCount(), "Liver", 2)
它产生以下输出:

这是可以预料的;没有地方明确地说我想把我的图片放到第1列

尝试2 当我尝试使用append方法添加并将位图设置为第二列项目时,如下所示:

self.health_grid = wx.ListCtrl(self, style=wx.LC_REPORT| wx.LC_ALIGN_TOP)
self.health_grid.InsertColumn(0, heading="Organ")
self.health_grid.InsertColumn(1, heading="Status")
self.health_icons = wx.ImageList(16, 16, False, 0)
self.unhealthy = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "unhealthy.png"))
self.health_icons.Add(self.unhealthy)
self.healthy = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "healthy.png"))
self.health_icons.Add(self.healthy)
self.unknown = wx.Bitmap(self.getFilePath("gui" + os.sep + "images" + os.sep + "unknown.png"))
self.health_icons.Add(self.unknown)
self.health_grid.SetImageList(self.health_icons, wx.IMAGE_LIST_SMALL)
self.health_grid.Append(["Heart", self.unhealthy])
它产生以下输出:

这稍微好一点;至少在第二列中添加了引用,但它仍然不是我想要的

尝试2.5 我进行了一次修改,试图使位图显示:

self.static_unhealthy = wx.StaticBitmap(self, -1, self.unhealthy)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.static_unhealthy, 0)
self.health_grid.Append(["Heart", self.sizer])
它产生了以下结果:

另一个错误的版本

尝试3 在以下代码中,我尝试显式地将图像指定给第二列,将文本指定给第一列:

item = wx.ListItem()
item.SetId(wx.NewId())
item.SetColumn(0)
item.SetText("Heart")
item.SetColumn(1)
item.SetImage(0)
self.health_grid.InsertItem(item)
然而,这产生了:

我想这是最接近我想要的,但仍然失败

最后
有可能生产出我想要的吗?我是否错过了一些明显的东西?

我所寻找的功能在Windows下是不可能实现的,因此我采用了以下不同的方法

使用wx.grid.grid而不是wx.ListCtrl,并实现了wx.grid.PyGridCellRenderer的重写版本,如下面的代码摘录所示:

import wx.grid as grid
import os
import wx

class healthStatus(wx.Frame):
    organs = ["heart", "lungs", "brain", "liver"]
    health_icons = []
    (row, progress) = (0, 0)

    def __init__(self, parent, id, title):
    # initialise the frame container
    wx.Frame.__init__(self, parent, id, title)
    # main gui sizer, shall be the sizer for the window
    self.main_sizer = wx.BoxSizer(wx.VERTICAL)

    self.health_grid = grid.Grid(self)
    self.health_grid.CreateGrid(len(self.organs),2)
    self.health_grid.SetColLabelValue(0, "Organ")
    self.health_grid.SetColLabelValue(1, "Status")
    self.health_grid.SetRowLabelSize(0)

    try:
        self.health_icons.append(self.getFilePath("images" + os.sep + "unknown.png"))
        self.health_icons.append(self.getFilePath("images" + os.sep + "unhealthy.png"))
        self.health_icons.append(self.getFilePath("images" + os.sep + "healthy.png"))
    except Exception as e:
        wx.MessageBox("Cannot load icons. \n Please ensure the images directory has not been moved\n\n"
                      + e.message, "Cannot Load Icons", wx.ICON_ERROR)
    index = 0
    for organ in self.organs:
        self.addItem(index, organ)
        index += 1
    self.main_sizer.Add(self.health_grid, 0, wx.ALL, 5)

    self.testing_button = wx.Button(self, wx.ID_ANY, "Testing")
    self.testing_button.Bind(wx.EVT_BUTTON, self.onProgress)
    self.main_sizer.Add(self.testing_button, 0, wx.ALL, 5)

    self.SetSizer(self.main_sizer)
    self.Fit()

    def addItem(self, index, organ):
    self.health_grid.SetCellValue(index, 0, organ)
    self.health_grid.SetCellRenderer(index, 1, BitmapRenderer(self.health_icons[0]))


    def updateProgress(self, index, progress):
    self.health_grid.SetCellRenderer(index, 1, BitmapRenderer(self.health_icons[progress]))
    self.health_grid.Refresh()

    def getFilePath(self, directory):
    curr = os.getcwd() + os.sep
    parent = "listctrlproblem" + os.sep
    parent_offset = len(parent)

    if curr.index(parent) + parent_offset != len(curr + os.sep):
        curr = curr[:curr.index(parent) + parent_offset]
        print curr
    return curr + os.sep + directory

    def onProgress(self, evt):        
    if self.health_grid.GetNumberRows() > 1:
        if self.progress + 1 < len(self.health_icons):
            self.progress += 1
        elif self.row < self.health_grid.GetNumberRows() + 1:
            self.progress = 0
            self.row +=1

    self.updateProgress(self.row, self.progress)


class BitmapRenderer(wx.grid.PyGridCellRenderer):
    def __init__(self, image):
    self.image = image
    wx.grid.PyGridCellRenderer.__init__(self)

    def Draw(self, grid, attr, dc, rect, row, col, is_selected):
    bmp = wx.Bitmap(self.image)
    dc.DrawBitmap(bmp, rect.X, rect.Y)

    def Clone(self):
    return self.__class__()


app = wx.App(False)
frame = healthStatus(None, -1, "Organ Status")
frame.Show(1)
app.MainLoop()
将wx.grid作为网格导入
导入操作系统
导入wx
类healthStatus(wx.Frame):
器官=[“心”、“肺”、“脑”、“肝”]
健康图标=[]
(行,进度)=(0,0)
定义初始化(自我、父项、id、标题):
#初始化框架容器
wx.Frame.\uuuuu init\uuuuuuuux(自我、父、id、标题)
#主gui尺寸测量仪应为窗口的尺寸测量仪
self.main_sizer=wx.BoxSizer(wx.VERTICAL)
self.health\u grid=grid.grid(self)
self.health\u grid.CreateGrid(len(self.organs),2)
自我健康网格设置协作值(0,“器官”)
self.health\u grid.SetColLabelValue(1,“状态”)
self.health\u grid.SetRowLabelSize(0)
尝试:
self.health_icons.append(self.getFilePath(“images”+os.sep+“unknown.png”))
self.health_icons.append(self.getFilePath(“images”+os.sep+“unhealth.png”))
self.health\u icons.append(self.getFilePath(“images”+os.sep+“health.png”))
例外情况除外,如e:
wx.MessageBox(“无法加载图标。\n请确保图像目录未被移动\n\n”
+e.消息“无法加载图标”,wx.图标(错误)
索引=0
对于自体器官中的器官:
自我补充(索引、器官)
指数+=1
self.main_sizer.Add(self.health_grid,0,wx.ALL,5)
self.testing_button=wx.button(self,wx.ID_ANY,“测试”)
self.testing_button.Bind(wx.EVT_button,self.onProgress)
self.main_sizer.Add(self.testing_按钮,0,wx.ALL,5)
自整定器(自整定器)
self.Fit()
def附加项(自身、索引、器官):
self.health_grid.SetCellValue(索引,0,器官)
self.health\u grid.SetCellRenderer(索引,1,位图渲染器(self.health\u图标[0]))
def updateProgress(自我、索引、进度):
self.health\u grid.SetCellRenderer(索引,1,位图渲染器(self.health\u图标[进度])
self.health_grid.Refresh()
def getFilePath(自身,目录):
curr=os.getcwd()+os.sep
parent=“listctrlproblem”+os.sep
父项偏移量=len(父项)
如果当前索引(父级)+父级偏移量!=len(当前+九月):
当前=当前[:当前索引(父项)+父项偏移]
印钞
返回curr+os.sep+目录
def onProgress(自我、evt):
如果self.health\u grid.GetNumberRows()>1:
如果self.progress+1
这将在Linux上创建以下输出:

以及Windows上的以下输出:


@MikeDriscoll我在Windows上看到了一个错误-它在self.health\u grid.SetItem(item)上失败,消息是:“C++断言”id>=0&&id