在wxpython核心部件中,wx.ComBox 是结合了一个单行文本字段 (field)、一个有着向下箭头图片的按钮以及一个列表箱 (listbox)的部件。
当按下按钮时,列表箱就会出现,用户只能从提供给他的字符串清单中选择一个选项。
wx.ComboBox 构建器:
wx.Size size=wx.DefaultSize, wx.List choices=wx.EmptyList,
int style=0, wx.Validator validator=wx.DefaultValidator,
string name=wx.ComboBoxNameStr)
wx.ComboBox 样式:
wx.CB_READONLY
wx.CB_SORT
如图:
wx.combobox.methods
例子:
#!/usr/bin/python
#coding=utf-8
#combobox.py
import wx
class MyDialog(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title,
size=(250, 270))
panel = wx.Panel(self, -1, (75, 20), (100, 127),
style=wx.SUNKEN_BORDER)
self.picture = wx.StaticBitmap(panel)
panel.SetBackgroundColour(wx.WHITE)
self.images = ['tolstoy.jpg',
'feuchtwanger.jpg',
'balzac.jpg',
'pasternak.jpg',
'galsworthy.jpg',
'wolfe.jpg',
'zweig.jpg']
authors = ['Leo Tolstoy', 'Lion Feuchtwanger',
'Honore de Balzac', 'Boris Pasternak',
'John Galsworthy', 'Tom Wolfe',
'Stefan Zweig']
wx.ComboBox(self, -1, pos=(50, 170), size=(150, -1),
choices=authors, style=wx.CB_READONLY)
wx.Button(self, 1, '关 闭', (80, 220))
self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
self.Center()
def OnClose(self, event):
self.Close()
def OnSelect(self, event):
item = event.GetSelection()
self.picture.SetFocus()
self.picture.SetBitmap(wx.Bitmap('images/' + self.images[item]))
class MyApp(wx.App):
def OnInit(self):
dlg = MyDialog(None, -1, 'combobox.py')
dlg.ShowModal()
dlg.Destroy()
return True
app = MyApp(0)
app.MainLoop()
如图:
图:combobox.py