Deletion in wxpython's list control

Your problem is that your list of items starts off with item [0] so
when you do current_items = GetCount it is set to 3 then you try to
delete item[3] out of a set of item[0], item[1], item[2] - and get a
message that there is no item[3] - not too surprising really.

···

On 23/01/13 07:50, Priyank Khare wrote:

My code is generating following error- *** “Couldn’t
retrieve information about list control item 3”***.

import wx
DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs
)

        self.panel = wx.Panel(self)

        self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
        self.list.InsertColumn(0, "Index")
        self.list.InsertColumn(1, "Number")
        for data in DATA:
            self.list.Append((data[0], data[1]))

        self.button = wx.Button(self.panel, label="Delete")
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
        self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
        self.panel.SetSizerAndFit(self.sizer)

        self.Show()

    def OnButton(self, e):
        current_items = self.list.GetItemCount()
        while ((current_items) >= 0) :
            if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items-1) == "2"):
                self.list.DeleteItem(current_items)
                wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
            else:
                break
            current_items-=1

if __name__ == "__main__":
    app = wx.App(False)
    win = MainWindow(None)
    win.Centre()
    app.MainLoop()
Can anyone tell me what is wrong with the code? And what should i do to resolve this error?
Thanks in advance.

  --

  To unsubscribe, send email to

or visit


Steve Gadget Barnes

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en

thank you Sir for reply.

The following code is working well:-

def OnButton(self, e):

current_items = self.list.GetItemCount() - 1

while ((current_items) >= 0) :

if (self.list.GetItemText(current_items) == “1” or self.list.GetItemText(current_items) == “2” or self.list.GetItemText(current_items) == “0”):

self.list.DeleteItem(current_items)

else:

break

current_items-=1

But problem is that in this, we give static conditions for just 3 items because there are only 3 items in the list.

So, if there will be 10-11 items in the list ,then in this case how we can delete these list items dynamically, then what will be the if condition…?

Please reply.

···

On Wednesday, January 23, 2013 2:14:05 PM UTC+5:30, Gadget Steve wrote:

On 23/01/13 07:50, Priyank Khare wrote:

My code is generating following error- *** “Couldn’t
retrieve information about list control item 3”***.

import wx
DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs
)

        self.panel = wx.Panel(self)

        self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
        self.list.InsertColumn(0, "Index")
        self.list.InsertColumn(1, "Number")
        for data in DATA:
            self.list.Append((data[0], data[1]))

        self.button = wx.Button(self.panel, label="Delete")
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
        self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
        self.panel.SetSizerAndFit(self.sizer)

        self.Show()

    def OnButton(self, e):
        current_items = self.list.GetItemCount()
        while ((current_items) >= 0) :
            if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items-1) == "2"):
                self.list.DeleteItem(current_items)
                wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
            else:
                break
            current_items-=1

if __name__ == "__main__":
    app = wx.App(False)
    win = MainWindow(None)
    win.Centre()
    app.MainLoop()
Can anyone tell me what is wrong with the code? And what should i do to resolve this error?
Thanks in advance.

  --

  To unsubscribe, send email to

wxPython-user...@googlegroups.com

  or visit [http://groups.google.com/group/wxPython-users?hl=en](http://groups.google.com/group/wxPython-users?hl=en)
Your problem is that your list of items starts off with item [0] so

when you do current_items = GetCount it is set to 3 then you try to
delete item[3] out of a set of item[0], item[1], item[2] - and get a
message that there is no item[3] - not too surprising really.


Steve Gadget Barnes


What about something like this (untested):
current_items = self.list.GetItemCount()
for x in range(current_items):
idx = current_items - x - 1
self.list.DeleteItem(idx)
Werner

···

On 23/01/2013 11:09, Priyank Khare
wrote:

          But problem is that in this, we give static conditions

for just 3 items because there are only 3 items in the
list.

So, if there will be 10-11 items in the list ,then in this case how we can delete
these list items dynamically, then what will be the if
condition…?

Please reply.

I am not quite sure what you are trying to achieve if your button
should empty the list then you do not need an while/if but you do
need a loop:
If you are just trying to delete the last item then just get rid of
the completely or replace it with a check that the length
of the item text is >0 to make sure you are not deleting empty
items. If you are deleting items selectively then your if will be
whatever matches your requirement to delete.
BTW this list - like many others - prefers bottom posting so that
you can read the question followed by the answer, (possibly followed
by follow-up question(s), etc.).

···

On 23/01/13 10:09, Priyank Khare wrote:

thank you Sir for reply.
The following code is working well:-

def OnButton(self, e):

*** current_items = self.list.GetItemCount()

  • 1***

while ((current_items) >= 0) :

*** if
(self.list.GetItemText(current_items) == “1” or
self.list.GetItemText(current_items) == “2” or
self.list.GetItemText(current_items) == “0”):***

self.list.DeleteItem(current_items)

else:

break

current_items-=1

          But problem is that in this, we give static conditions

for just 3 items because there are only 3 items in the
list.

So, if there will be 10-11 items in the list ,then in this case how we can delete
these list items dynamically, then what will be the if
condition…?

Please reply.

for n in range(0, current_items, -1): self.list.DeleteItem(n)
if


Steve Gadget Barnes


Much nicer then what I had suggested, didn’t know I can do this with
range, but for me the “-1” doesn’t work, had to do this:
for n in range(0, current_items, 1):
self.list.DeleteItem(n)
Werner

···

On 24/01/2013 08:54, Steve Barnes
wrote:

  I am not quite sure what you are trying to achieve if

your button should empty the list then you do not need an while/if
but you do need a loop:
for n in range(0, current_items, -1): self.list.DeleteItem(n)

    On 23/01/13 10:09, Priyank Khare

wrote:

Sorry the code should have been:
As you can get some problems deleting from the beginning of a list,
as things are shuffled down the list.

···

On 24/01/13 08:28, Werner wrote:

  ...

Much nicer then what I had suggested, didn’t know I can do this
with range, but for me the “-1” doesn’t work, had to do this:
for n in range(0, current_items, 1):
self.list.DeleteItem(n)
Werner
– – To unsubscribe, send email to
or visit

for n in range(current_items, -1, -1): self.list.DeleteItem(n)


Steve Gadget Barnes

    On 24/01/2013 08:54, Steve Barnes

wrote:

      On 23/01/13 10:09, Priyank Khare

wrote:

    I am not quite sure what you are trying to achieve

if your button should empty the list then you do not need an
while/if but you do need a loop:
for n in range(0, current_items, -1): self.list.DeleteItem(n)

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en

Thanks, it is working in my small application. But not working in my main application.

Actually, i am making a application which generates scripts through different key presses and capture images.

Let me try to explain you…My application will work like as follows:-

1. Whenever i clicks on Start button of application, it will do 2 things:-

(a) Delete entries if any hotkey(CTRL/SHFT/ALT) pressed during the script start& check for the last three

occurrences for hotkey & its modifier key down and wait(for time, not the wait image command) events

generated.

For this purpose my code is (working well) :-

for r in range(0,3):

        prev_item = self.logwin.GetItemText(self.logwin.ItemCount-1)

        if prev_item == "keyDown(Key."+func_key+")" or prev_item == "keyDown(Key."+key_mod+")" or (prev_item.split('(')[0]=='wait' and prev_item.split('("')[0]!='wait'):

            logging.debug('Deleting logged hotkey action: '+prev_item)

            self.logwin.DeleteItem(self.logwin.ItemCount-1)

    current_items = self.logwin.GetItemCount()-1

(b) Secondly, Keep on deleting all the preceding continuous key modifier & wait events generated during long

presses for different hotkey actions and remove the entries that match this criteria.

For this purpose my code is:-

while ((current_items) >= 0) :

            key_mod_down = "keyDown(Key."+key_mod+")"

            if self.logwin.GetItemText(current_items) == key_mod_down or self.logwin.GetItemText(current_items-1) == key_mod_down:

                logging.debug("Removing: " + self.logwin.GetItemText(current_items))

                self.logwin.DeleteItem(current_items)

            else:

                break

            current_items-=1

***“in this section error is generating-Couldn’t retrieve information about list control item 0” ***

2. After the above steps, it should be display my operations for capturing images.

" This step is working well. The operations are displaying ", but with the error.

So, i want to know why this “while” loop not deleting the item of 0 index.

Now, please, help me suggest what should i do to resolve this…?

···

On Thursday, January 24, 2013 4:15:02 PM UTC+5:30, Gadget Steve wrote:

On 24/01/13 08:28, Werner wrote:

    On 24/01/2013 08:54, Steve Barnes > > wrote:
      On 23/01/13 10:09, Priyank Khare > > > wrote:

    I am not quite sure what you are trying to achieve

if your button should empty the list then you do not need an
while/if but you do need a loop:
for n in range(0, current_items, -1): self.list.DeleteItem(n)
Much nicer then what I had suggested, didn’t know I can do this
with range, but for me the “-1” doesn’t work, had to do this:

  for n in range(0, current_items, 1):

      self.list.DeleteItem(n)



  Werner

  --

  --

  To unsubscribe, send email to

wxPython-user...@googlegroups.com

  or visit [http://groups.google.com/group/wxPython-users?hl=en](http://groups.google.com/group/wxPython-users?hl=en)

Sorry the code should have been:
for n in range(current_items, -1, -1): self.list.DeleteItem(n)

As you can get some problems deleting from the beginning of a list,

as things are shuffled down the list.

  Steve *Gadget* Barnes

I think that your problem is that horrid statement when
you get to current_item = 0 you are checking:
which of course is an error as there is no such item!

···

On 24/01/13 11:40, Priyank Khare wrote:

  Thanks, it is working in my small application. But not

working in my main application.
Actually, i am
making a application which generates scripts through different
key presses and capture images.

      Let me try to

explain you…My application will work like as follows:-

1 . Whenever
i clicks on Start button of application, it will do 2 things:-

(a) Delete
entries if any hotkey(CTRL/SHFT/ALT) pressed during the script
start& check for the last three

      occurrences

for hotkey & its modifier key down and wait(for time, not
the wait image command) events

generated.

      For this

purpose my code is (working well) :-

for r in range(0,3):

                    prev_item =

self.logwin.GetItemText(self.logwin.ItemCount-1)

                    if prev_item ==

“keyDown(Key.”+func_key+“)” or prev_item ==
“keyDown(Key.”+key_mod+“)” or (prev_item.split(‘(’)[0]==’ wait’
and prev_item.split(‘("’)[0]!=‘wait’):

                        logging.debug('Deleting

logged hotkey action: '+prev_item)

            self.logwin.DeleteItem(self.logwin.ItemCount-1)
                current_items =

self.logwin.GetItemCount()-1

(b)
Secondly, Keep on deleting all the preceding continuous key
modifier & wait events generated during long

      presses for

different hotkey actions and remove the entries that match
this criteria.

      For this

purpose my code is:-

while ((current_items) >= 0) :

                        key_mod_down =

“keyDown(Key.”+key_mod+“)”

            if self.logwin.GetItemText(            current_items)

== key_mod_down or self.logwin.GetItemText( current_items-1)
== key_mod_down:

                            logging.debug("Removing:

" + self.logwin.GetItemText(current_items))

                self.logwin.DeleteItem(current_items)
            else:
                break
            current_items-=1

***“in this section error is generating- Couldn’t
retrieve information about list control item 0” ***

2. After the
above steps, it should be display my operations for capturing
images.

      " This step is

working well. The operations are displaying ", but with the
error.

      So, i want to know

why this “while” loop not deleting the item of 0 index.

      Now, please, help

me suggest what should i do to resolve this…?

    On Thursday, January 24, 2013 4:15:02 PM UTC+5:30, Gadget Steve

wrote:

On 24/01/13 08:28, Werner wrote:

On 24/01/2013 08:54, Steve Barnes wrote:

On 23/01/13 10:09, Priyank Khare wrote:

            I am not quite sure what you are

trying to achieve if your button should empty the list
then you do not need an while/if but you do need a loop:
for n in range(0, current_items, -1): self.list.DeleteItem(n)
Much nicer then what I had suggested, didn’t know I can do
this with range, but for me the “-1” doesn’t work, had to
do this:

          for n in range(0, current_items, 1):

              self.list.DeleteItem(n)



          Werner

          --

          --

          To unsubscribe, send email to wxPython-user...@googlegroups.com

          or visit [http://groups.google.com/group/wxPython-users?hl=en](http://groups.google.com/group/wxPython-users?hl=en)

Sorry the code should have been:
for n in range(current_items, -1, -1): self.list.DeleteItem(n)

        As you can get some problems deleting from the beginning of

a list, as things are shuffled down the list.

          Steve *Gadget* Barnes

  --

  To unsubscribe, send email to

or visit
if
if self.logwin.GetItemText(0) ==
key_mod_down or self.logwin.GetItemText(-1 ) ==
key_mod_down:


Steve Gadget Barnes

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en

What happens if you delete the last item? ItemCount will be 0, and
your first line will try to refer to GetItemText(-1).
Why is the “-1” in the last line?
You can write that first line as “for r in range(3):”
Again, when current_items is 0, your “if” statement will try to call
GetItemText(-1).
That first line can be written without any parentheses at all. This
is not C.

···

Priyank Khare wrote:

  Thanks, it is working in my small application. But not

working in my main application.
Actually, i am
making a application which generates scripts through different
key presses and capture images.

      Let me try to

explain you…My application will work like as follows:-

1 . Whenever
i clicks on Start button of application, it will do 2 things:-

(a) Delete
entries if any hotkey(CTRL/SHFT/ALT) pressed during the script
start& check for the last three

      occurrences

for hotkey & its modifier key down and wait(for time, not
the wait image command) events

generated.

      For this

purpose my code is (working well) :-

for r in range(0,3):

                    prev_item =

self.logwin.GetItemText(self.logwin.ItemCount-1)

                    if prev_item ==

“keyDown(Key.”+func_key+“)” or prev_item ==
“keyDown(Key.”+key_mod+“)” or (prev_item.split(‘(’)[0]==’ wait’
and prev_item.split(‘("’)[0]!=‘wait’):

                        logging.debug('Deleting

logged hotkey action: '+prev_item)

            self.logwin.DeleteItem(self.logwin.ItemCount-1)
                current_items =

self.logwin.GetItemCount()-1

(b)
Secondly, Keep on deleting all the preceding continuous key
modifier & wait events generated during long

      presses for

different hotkey actions and remove the entries that match
this criteria.

      For this

purpose my code is:-

while ((current_items) >= 0) :

                        key_mod_down =

“keyDown(Key.”+key_mod+“)”

            if self.logwin.GetItemText(            current_items)

== key_mod_down or self.logwin.GetItemText( current_items-1)
== key_mod_down:

                            logging.debug("Removing:

" + self.logwin.GetItemText(current_items))

                self.logwin.DeleteItem(current_items)
            else:
                break
            current_items-=1

***“in this section error is generating- Couldn’t
retrieve information about list control item 0” ***

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Steve Barnes wrote:

I am not quite sure what you are trying to achieve if your button should
empty the list then you do not need an while/if but you do need a loop:
*for n in range(0, current_items, -1):**
**self.list.DeleteItem(n)*

Or just

     self.list.DeleteAllItems()

···

--
Robin Dunn
Software Craftsman