Dimitris Karvounis
Dimitris Karvounis

Reputation: 29

How to delete Items on wx.listCtrl from another frame?

I am running in trouble with wxPython

I have this code

class MyForm(wx.Frame):
# ----------------------------------------------------------------------
def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Lapop - Αδειες Υπαλλήλων", size = (700,400))

    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    self.list_ctrl = wx.ListCtrl(panel, size=(680, 340),
                                 style=wx.LC_REPORT
                                       | wx.BORDER_SUNKEN
                                 )
    self.list_ctrl.Bind(wx.EVT_COMMAND_LEFT_DCLICK, self.DoubleClick)
    self.list_ctrl.InsertColumn(0, 'ID',width=40)
    self.list_ctrl.InsertColumn(1, 'Name', width=250)
    self.list_ctrl.InsertColumn(2, 'Row1', width=150)
    self.list_ctrl.InsertColumn(3, 'Row2', width=150)

    sizer.Add(self.list_ctrl, 0, wx.ALL | wx.EXPAND, 5)


    panel.SetSizer(sizer)

# ----------------------------------------------------------------------

def UpdateListView(self):
    self.list_ctrl.DeleteAllItems()
    print self.list_ctrl.GetItemCount()

And from another class (another wx.Frame), I try to update the list on MyForm frame.

MyForm().UpdateListView()

Although I get the count of items in list, but unfortunatelly, I can't delete the items.

Any ideas?

Upvotes: 0

Views: 1586

Answers (2)

Dimitris Karvounis
Dimitris Karvounis

Reputation: 29

I found the solution.

Firstly, include pubsub

from wx.lib.pubsub import pub

Then, I neet to set the subscribe, right after the creating of ListCtrl

self.list_ctrl = wx.ListCtrl(panel, size=(680, 340),
                                 style=wx.LC_REPORT
                                       | wx.BORDER_SUNKEN
                                 )
self.list_ctrl.Bind(wx.EVT_COMMAND_LEFT_DCLICK, self.DoubleClick)
pub.subscribe(self.UpdateListView, 'UpdateListview')

also, I need to make the function inside same class of ListCtrl creation.

    def UpdateListView(self):
    self.list_ctrl.DeleteAllItems()
    #Do something else

Then, from any other class you just send a message to update the list.

pub.sendMessage('UpdateListview')

Upvotes: 1

RobinDunn
RobinDunn

Reputation: 6206

MyForm().UpdateListView() will create a new instance of MyForm, not give you a reference to the existing one. To do that you just need to give your other frame a reference, or some way to access the reference to the existing MyForm.

Upvotes: 0

Related Questions