I basically need to make the repeat button work.

I've tried several approaches but i've hit a brick wall and there doesn't seem to be a function I can find to get the values of the selected item and repeat them.

self.listctrl.GetItemString() will only get the string from item in the first column, and ideally I need to also be able to pick which column I would like the string from.

Does anyone either know how I can repeat the entire item or a GetString(row, column)type function because im straight out of ideas now.

Thanks.

import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
import sys

class AutoWidthListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin):
    def __init__(self, parent):
        
        wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT)
        ListCtrlAutoWidthMixin.__init__(self)

class MyFrame(wx.Frame):

    def __init__(self, parent, id):
    
        wx.Frame.__init__(self, parent, id, 'Quick Silver', size = (330, 500))

        panel = wx.Panel(self)

        self.listctrl = AutoWidthListCtrl(panel)
        self.listctrl.InsertColumn(0, "Item", width = 230)
        self.listctrl.InsertColumn(1, "Price")

        for x in range(1,15):

            index = self.listctrl.InsertStringItem(sys.maxint, str(x))
            self.listctrl.SetStringItem(index, 1, str(x))

        self.repbtn = wx.Button(panel, label = "Repeat Item")
        self.Bind(wx.EVT_BUTTON, self.RepeatItem, self.repbtn)
        self.delbtn = wx.Button(panel, label = "Delete Item")
        self.Bind(wx.EVT_BUTTON, self.RemoveItem, self.delbtn)

        specbtns = wx.BoxSizer()
        specbtns.Add(self.repbtn, 1, wx.EXPAND)
        specbtns.Add(self.delbtn, 1, wx.EXPAND)

        holder = wx.BoxSizer(wx.VERTICAL)
        holder.Add(self.listctrl, 7, flag = wx.EXPAND)
        holder.Add(specbtns, 1, flag = wx.EXPAND)

        panel.SetSizer(holder)
            
    def RemoveItem(self, event):

        self.listctrl.DeleteItem(self.listctrl.GetFocusedItem())


    def RepeatItem(self, event):

        self.listctrl.Append(self.listctrl.GetFocusedItem())        

app = wx.App(redirect = False)
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()

Dani AI

Generated

Short answer: the thing printed in Post #2 is a wx.ListItem proxy and calling GetText() on it can return nothing unless the control is asked to populate the subitem text. On modern wxPython you can call GetItemText(row, col) (the col argument was added in wxWidgets 2.9.1); on older builds you must create a wx.ListItem, set its id/column and request the text with a text mask before calling GetItem. (docs.wxpython.org)

A small helper that works with both new and old APIs is shown below. It first tries the simple GetItemText(row, col) and falls back to a wx.ListItem + mask approach if that call is not available.

def get_cell_text(listctrl, row, col=0):
    try:
        return listctrl.GetItemText(row, col)
    except TypeError:
        li = wx.ListItem()
        li.SetId(row)
        li.SetColumn(col)
        li.SetMask(wx.LIST_MASK_TEXT)
        listctrl.GetItem(li)
        return li.GetText()

To duplicate the focused/selected row(s) copy every column value and insert a new row. Use GetFirstSelected / GetNextSelected to iterate selected items, InsertItem to add the first column and SetItem (or SetItem overload) for the subitems. Example:

def duplicate_selected(listctrl):
    idx = listctrl.GetFirstSelected()
    while idx != -1:
        cols = listctrl.GetColumnCount()
        values = [get_cell_text(listctrl, idx, c) for c in range(cols)]
        new = listctrl.InsertItem(listctrl.GetItemCount(), values[0])
        for c in range(1, cols):
            listctrl.SetItem(new, c, values[c])
        idx = listctrl.GetNextSelected(idx)

This uses the supported insert/set APIs and the selection helpers. (docs.wxpython.org)

Notes and cautions: if the control is virtual (LC_VIRTUAL) the UI asks your data source for text (you must update the underlying data model and/or implement OnGetItemText), so the above direct copying won’t apply; also remember to copy any associated item data or images if those are used. (docs.wxpython.org)

In ’s case the blank GetText() came from not using either the GetItemText(row, col) convenience or the wx.ListItem + mask pattern; the helper above will produce the expected subitem text for duplication.

print self.listctrl.GetItem(6,2)

returns

<wx._controls.ListItem; proxy of <Swig Object of type 'wxListItem *' at 0x2f0e3b0> >


am I using it wrong?

the answer is apparently to use .GetItem() then GetItemText so for example

x = self.listctrl.GetItem(6,2)
text = x.GetText()

but it just prints a blank line into the editor for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.