[Python] wxPython listbox 使用演示 →→→→→进入此内容的聊天室

来自 , 2020-05-18, 写在 Python, 查看 154 次.
URL http://www.code666.cn/view/be3b0b54
  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. # If you have not already done so, install Python 2.4 first.
  5. # I used  python-2.4.1c2.msi  (this is the self-extracting
  6. # MS-Installer file) from http://www.python.org
  7. # Then install  wxPython2.5-win32-unicode-2.5.4.1-py24.exe
  8. # from: http://prdownloads.sourceforge.net/wxpython/
  9. # (if you don't get into unicode, download the ansi version)
  10. # note: python-2.4.1c2.msi  should soon be python-2.4.1.msi
  11.  
  12. import wx
  13. def create(parent):
  14.     return Frame1(parent)
  15. # assign ID numbers
  16. [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1LISTBOX1,
  17. ] = [wx.NewId() for _init_ctrls in range(4)]
  18. class Frame1(wx.Frame):
  19.     def _init_ctrls(self, prnt):
  20.         # BOA generated methods
  21.         wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  22.               pos=wx.Point(358, 184), size=wx.Size(299, 387),
  23.               style=wx.DEFAULT_FRAME_STYLE, title=u'ListBox Test ...')
  24.         self.SetClientSize(wx.Size(291, 347))
  25.         self.SetBackgroundColour(wx.Colour(0, 128, 0))
  26.         self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'Load ListBox',
  27.               name='button1', parent=self, pos=wx.Point(8, 8), size=wx.Size(176,
  28.               28), style=0)
  29.         self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
  30.               id=wxID_FRAME1BUTTON1)
  31.         self.listBox1 = wx.ListBox(choices=[], id=wxID_FRAME1LISTBOX1,
  32.               name='listBox1', parent=self, pos=wx.Point(8, 48),
  33.               size=wx.Size(184, 256), style=0)
  34.         self.listBox1.SetBackgroundColour(wx.Colour(255, 255, 128))
  35.         self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox,
  36.               id=wxID_FRAME1LISTBOX1)
  37.         self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label=u'Clear',
  38.               name='button2', parent=self, pos=wx.Point(104, 312),
  39.               size=wx.Size(87, 28), style=0)
  40.         self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
  41.               id=wxID_FRAME1BUTTON2)
  42.     def __init__(self, parent):
  43.         self._init_ctrls(parent)
  44.     def OnButton1Button(self, event):
  45.         '''
  46.        click button to load the listbox with names
  47.        '''
  48.         self.listBox1.Append("Andreas")
  49.         self.listBox1.Append("Erich")
  50.         self.listBox1.Append("Udo")
  51.         self.listBox1.Append("Jens")
  52.         self.listBox1.Append("Bjorn")
  53.         self.listBox1.Append("Heidrun")
  54.         self.listBox1.Append("Ulla")
  55.         self.listBox1.Append("Volger")
  56.         self.listBox1.Append("Helmut")
  57.         self.listBox1.Append("Freja")
  58.         self.SetTitle("Select a name ...")
  59.    
  60.     def OnListBox1Listbox(self, event):
  61.         '''
  62.        click list item and display the selected string in frame's title
  63.        '''
  64.         selName = self.listBox1.GetStringSelection()
  65.         self.SetTitle(selName)
  66.        
  67.     def OnButton2Button(self, event):
  68.         '''
  69.        click button to clear the listbox items
  70.        '''
  71.         self.listBox1.Clear()
  72. #--------------- end of class Frame1 --------------------
  73. # program entry point ...
  74. if __name__ == '__main__':
  75.     app = wx.PySimpleApp()
  76.     wx.InitAllImageHandlers()
  77.     frame = create(None)
  78.     frame.Show()
  79.     app.MainLoop()
  80. #//python/5737

回复 "wxPython listbox 使用演示"

这儿你可以回复上面这条便签

captcha