wxpython中listbox用法实例详解

本文实例讲述了wxpython中listbox用法。分享给大家供大家参考。具体如下:

# load a listbox with names, select a name and display in title
# experiments with wxpython by vegaseat 20mar2005
# python v2.4 and wxpython v2.5
# if you have not already done so, install python 2.4 first.
# i used python-2.4.1c2.msi (this is the self-extracting
# ms-installer file) from http://www.python.org
# then install wxpython2.5-win32-unicode-2.5.4.1-py24.exe
# from: http://prdownloads.sourceforge.net/wxpython/
# (if you don’t get into unicode, download the ansi version)
# note: python-2.4.1c2.msi should soon be python-2.4.1.msi
import wx
def create(parent):
return frame1(parent)
# assign id numbers
[wxid_frame1, wxid_frame1button1, wxid_frame1button2, wxid_frame1listbox1,
] = [wx.newid() for _init_ctrls in range(4)]
class frame1(wx.frame):
def _init_ctrls(self, prnt):
# boa generated methods
wx.frame.__init__(self, ‘, parent=prnt,
pos=wx.point(358, 184), size=wx.size(299, 387),
listbox test …’)
self.setclientsize(wx.size(291, 347))
self.setbackgroundcolour(wx.colour(0, 128, 0))
self.button1 = wx.button(load listbox’,
name=’button1′, parent=self, pos=wx.point(8, 8), size=wx.size(176,
28), style=0)
self.button1.bind(wx.evt_button, self.onbutton1button,
id=wxid_frame1button1)
self.listbox1 = wx.listbox(choices=[], id=wxid_frame1listbox1,
name=’listbox1′, parent=self, pos=wx.point(8, 48),
size=wx.size(184, 256), style=0)
self.listbox1.setbackgroundcolour(wx.colour(255, 255, 128))
self.listbox1.bind(wx.evt_listbox, self.onlistbox1listbox,
id=wxid_frame1listbox1)
self.button2 = wx.button(clear’,
name=’button2′, parent=self, pos=wx.point(104, 312),
size=wx.size(87, 28), style=0)
self.button2.bind(wx.evt_button, self.onbutton2button,
id=wxid_frame1button2)
def __init__(self, parent):
self._init_ctrls(parent)
def onbutton1button(self, event):
”’
click button to load the listbox with names
”’
self.listbox1.append(“andreas”)
self.listbox1.append(“erich”)
self.listbox1.append(“udo”)
self.listbox1.append(“jens”)
self.listbox1.append(“bjorn”)
self.listbox1.append(“heidrun”)
self.listbox1.append(“ulla”)
self.listbox1.append(“volger”)
self.listbox1.append(“helmut”)
self.listbox1.append(“freja”)
self.settitle(“select a name …”)
def onlistbox1listbox(self, event):
”’
click list item and display the selected string in frame’s title
”’
selname = self.listbox1.getstringselection()
self.settitle(selname)
def onbutton2button(self, event):
”’
click button to clear the listbox items
”’
self.listbox1.clear()
#————— end of class frame1 ——————–
# program entry point …
if __name__ == ‘__main__’:
app = wx.pysimpleapp()
wx.initallimagehandlers()
frame = create(none)
frame.show()
app.mainloop()

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类