In this Python GUI article i want to show Creating ListBox in wxPython. so this will be a simple project that a user can add, delete and edit a ListBox.
a listbox is used to select one or more of a list of strings. so The strings are displayed in a scrolling box, with the selected string(s) marked in reverse video. a listbox can be single selection (if an item is selected, the previous selection is removed) or multiple selection (clicking an item toggles the item on or off independently of other selections).
This class supports the following styles:
wx.LB_SINGLE: So it is a Single-selection list.wx.LB_MULTIPLE: Multiple-selection list: the user can toggle multiple items on and off. This is the same aswx.LB_EXTENDEDin wxGTK2 port.wx.LB_EXTENDED: Extended-selection list: the user can extend the selection by usingSHIFTorCTRLkeys together with the cursor movement keys or the mouse.wx.LB_HSCROLL: Create horizontal scrollbar if contents are too wide (Windows only).wx.LB_ALWAYS_SB: Always show a vertical scrollbar.wx.LB_NEEDED_SB: Only create a vertical scrollbar if needed.wx.LB_NO_SB: Don’t create vertical scrollbar (wxMSW and wxGTK only).wx.LB_SORT: The listbox contents are sorted in alphabetical order.
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
So this is the complete code for Python GUI Creating ListBox in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) hbox = wx.BoxSizer(wx.HORIZONTAL) self.listbox = wx.ListBox(self) hbox.Add(self.listbox, wx.ID_ANY, wx.EXPAND | wx.ALL, 20) btnPanel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) newBtn = wx.Button(btnPanel, wx.ID_ANY, 'New', size=(90, 30)) renBtn = wx.Button(btnPanel, wx.ID_ANY, 'Rename', size=(90, 30)) delBtn = wx.Button(btnPanel, wx.ID_ANY, 'Delete', size=(90, 30)) clrBtn = wx.Button(btnPanel, wx.ID_ANY, 'Clear', size=(90, 30)) self.Bind(wx.EVT_BUTTON, self.NewItem, id=newBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRename, id=renBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnClear, id=clrBtn.GetId()) self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnRename) vbox.Add((-1, 20)) vbox.Add(newBtn) vbox.Add(renBtn, 0, wx.TOP, 5) vbox.Add(delBtn, 0, wx.TOP, 5) vbox.Add(clrBtn, 0, wx.TOP, 5) btnPanel.SetSizer(vbox) hbox.Add(btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20) self.SetSizer(hbox) self.Centre() def NewItem(self, event): text = wx.GetTextFromUser('Enter a new item', 'Insert dialog') if text != '': self.listbox.Append(text) def OnRename(self, event): sel = self.listbox.GetSelection() text = self.listbox.GetString(sel) renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text) if renamed != '': self.listbox.Delete(sel) item_id = self.listbox.Insert(renamed, sel) self.listbox.SetSelection(item_id) def OnDelete(self, event): sel = self.listbox.GetSelection() if sel != -1: self.listbox.Delete(sel) def OnClear(self, event): self.listbox.Clear() class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="List Box") self.frame.Show() return True app = MyApp() app.MainLoop() |
This is class is MyFrame class that inherits from wx.Frame and it is our top level window that we create our MyPanel object in this class
1 2 3 4 5 6 | class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) self.panel = MyPanel(self) |
This is our MyPanel class and we create our all widgets, layouts and bindings in this class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) hbox = wx.BoxSizer(wx.HORIZONTAL) self.listbox = wx.ListBox(self) hbox.Add(self.listbox, wx.ID_ANY, wx.EXPAND | wx.ALL, 20) btnPanel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) newBtn = wx.Button(btnPanel, wx.ID_ANY, 'New', size=(90, 30)) renBtn = wx.Button(btnPanel, wx.ID_ANY, 'Rename', size=(90, 30)) delBtn = wx.Button(btnPanel, wx.ID_ANY, 'Delete', size=(90, 30)) clrBtn = wx.Button(btnPanel, wx.ID_ANY, 'Clear', size=(90, 30)) self.Bind(wx.EVT_BUTTON, self.NewItem, id=newBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRename, id=renBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnClear, id=clrBtn.GetId()) self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnRename) vbox.Add((-1, 20)) vbox.Add(newBtn) vbox.Add(renBtn, 0, wx.TOP, 5) vbox.Add(delBtn, 0, wx.TOP, 5) vbox.Add(clrBtn, 0, wx.TOP, 5) btnPanel.SetSizer(vbox) hbox.Add(btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20) self.SetSizer(hbox) self.Centre() |
And this is the the creation of our HBox Sizer and ListBox
1 2 3 | hbox = wx.BoxSizer(wx.HORIZONTAL) self.listbox = wx.ListBox(self) hbox.Add(self.listbox, wx.ID_ANY, wx.EXPAND | wx.ALL, 20) |
It is time to create our btnPanel because we are going to add some buttons in this panel
1 2 3 4 5 6 | btnPanel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) newBtn = wx.Button(btnPanel, wx.ID_ANY, 'New', size=(90, 30)) renBtn = wx.Button(btnPanel, wx.ID_ANY, 'Rename', size=(90, 30)) delBtn = wx.Button(btnPanel, wx.ID_ANY, 'Delete', size=(90, 30)) clrBtn = wx.Button(btnPanel, wx.ID_ANY, 'Clear', size=(90, 30)) |
These are event bindings for our buttons
1 2 3 4 5 | self.Bind(wx.EVT_BUTTON, self.NewItem, id=newBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRename, id=renBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnClear, id=clrBtn.GetId()) self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnRename) |
So these are the methods that we have used for binding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | def NewItem(self, event): text = wx.GetTextFromUser('Enter a new item', 'Insert dialog') if text != '': self.listbox.Append(text) def OnRename(self, event): sel = self.listbox.GetSelection() text = self.listbox.GetString(sel) renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text) if renamed != '': self.listbox.Delete(sel) item_id = self.listbox.Insert(renamed, sel) self.listbox.SetSelection(item_id) def OnDelete(self, event): sel = self.listbox.GetSelection() if sel != -1: self.listbox.Delete(sel) def OnClear(self, event): self.listbox.Clear() |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. when all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 | class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="List Box") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
