wxPython ListBox Demo

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Mar 20th, 2005, 10:34 pm |
0
The wxPython library allows Python to create Windows GUI programs. Here is a look at the listbox component. Click a button to load the listbox with names, select the name by clicking on it on the list, and display it in the title bar of the window frame. Another button clears the listbox.
Quick reply to this message  
Python Syntax
  1. # load a listbox with names, select a name and display in title
  2. # experiments with wxPython by vegaseat 20mar2005
  3. # Python v2.4 and wxPython v2.5
  4.  
  5. # If you have not already done so, install Python 2.4 first.
  6. # I used python-2.4.1c2.msi (this is the self-extracting
  7. # MS-Installer file) from http://www.python.org
  8. # Then install wxPython2.5-win32-unicode-2.5.4.1-py24.exe
  9. # from: http://prdownloads.sourceforge.net/wxpython/
  10. # (if you don't get into unicode, download the ansi version)
  11. # note: python-2.4.1c2.msi should soon be python-2.4.1.msi
  12.  
  13. import wx
  14.  
  15. def create(parent):
  16. return Frame1(parent)
  17.  
  18. # assign ID numbers
  19. [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1LISTBOX1,
  20. ] = [wx.NewId() for _init_ctrls in range(4)]
  21.  
  22. class Frame1(wx.Frame):
  23. def _init_ctrls(self, prnt):
  24. # BOA generated methods
  25. wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  26. pos=wx.Point(358, 184), size=wx.Size(299, 387),
  27. style=wx.DEFAULT_FRAME_STYLE, title=u'ListBox Test ...')
  28. self.SetClientSize(wx.Size(291, 347))
  29. self.SetBackgroundColour(wx.Colour(0, 128, 0))
  30.  
  31. self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'Load ListBox',
  32. name='button1', parent=self, pos=wx.Point(8, 8), size=wx.Size(176,
  33. 28), style=0)
  34. self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
  35. id=wxID_FRAME1BUTTON1)
  36.  
  37. self.listBox1 = wx.ListBox(choices=[], id=wxID_FRAME1LISTBOX1,
  38. name='listBox1', parent=self, pos=wx.Point(8, 48),
  39. size=wx.Size(184, 256), style=0)
  40. self.listBox1.SetBackgroundColour(wx.Colour(255, 255, 128))
  41. self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox,
  42. id=wxID_FRAME1LISTBOX1)
  43.  
  44. self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label=u'Clear',
  45. name='button2', parent=self, pos=wx.Point(104, 312),
  46. size=wx.Size(87, 28), style=0)
  47. self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
  48. id=wxID_FRAME1BUTTON2)
  49.  
  50. def __init__(self, parent):
  51. self._init_ctrls(parent)
  52.  
  53. def OnButton1Button(self, event):
  54. '''
  55. click button to load the listbox with names
  56. '''
  57. self.listBox1.Append("Andreas")
  58. self.listBox1.Append("Erich")
  59. self.listBox1.Append("Udo")
  60. self.listBox1.Append("Jens")
  61. self.listBox1.Append("Bjorn")
  62. self.listBox1.Append("Heidrun")
  63. self.listBox1.Append("Ulla")
  64. self.listBox1.Append("Volger")
  65. self.listBox1.Append("Helmut")
  66. self.listBox1.Append("Freja")
  67. self.SetTitle("Select a name ...")
  68.  
  69. def OnListBox1Listbox(self, event):
  70. '''
  71. click list item and display the selected string in frame's title
  72. '''
  73. selName = self.listBox1.GetStringSelection()
  74. self.SetTitle(selName)
  75.  
  76. def OnButton2Button(self, event):
  77. '''
  78. click button to clear the listbox items
  79. '''
  80. self.listBox1.Clear()
  81.  
  82. #--------------- end of class Frame1 --------------------
  83.  
  84. # program entry point ...
  85. if __name__ == '__main__':
  86. app = wx.PySimpleApp()
  87. wx.InitAllImageHandlers()
  88. frame = create(None)
  89. frame.Show()
  90.  
  91. app.MainLoop()
  92.  
0
vegaseat vegaseat is offline Offline | Mar 25th, 2005
You can run this program with version 2.3 of Python, but you need version 2.5 of wxPython (introduces bind)!
 
0
vegaseat vegaseat is offline Offline | Mar 30th, 2005
Python does support unicode (slavic, chinese etc. characters), but if you never get into foreign languages, you can simply download the ansi flavor of wxPython. Ansi does support french, german, spanish, and some greek characters.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC