1

I have 3 controls in my frame -

  1. A list box that has a list of employees name from the employee table.
  2. A text box that accepts new employee name
  3. A command button on click will insert a new name in the employee table.

Requirement:

Once I press the submit button after inserting a new row the list box should be automatically refreshed with the new name.

How do I accomplish this task ?

I'm successfully able to create the controls and bind a on click event and insert a row. But unable to refresh the list box.

Thanks for your help in advance.

1 Answer 1

1

You should use the ListBox's SetItems method:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.choices = ["George Lucas"]
        self.lbox = wx.ListBox(self, choices=self.choices)
        self.new_emp = wx.TextCtrl(self)
        addBtn = wx.Button(self, label="Add Employee")
        addBtn.Bind(wx.EVT_BUTTON, self.addEmployee)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.lbox, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(self.new_emp, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(addBtn, 0, wx.ALL, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def addEmployee(self, event):
        """"""
        emp = self.new_emp.GetValue()
        self.choices.append(emp)
        self.lbox.SetItems(self.choices)
        self.new_emp.SetValue("")

########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Employee")
        panel = MyPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.