13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced widgetshttp://www.zetcode.com/wxpython/advanced/<strong>The</strong> constructor of a wx.ListBox widget is as follows:wx.ListBox(wx.Window parent, int id=-1, wx.Point pos=wx.DefaultPosition, wx.Size size=wx.DefaultSlist choices=[], long style=0, wx.Validator validator=wx.DefaultValidator,string name=wx.ListBoxNameStr)<strong>The</strong>re is a choices parameter. If we put some values there, they will bedisplayed from the construction of the widget. This parameter is empty bydefault.In our code example we have a listbox and four buttons. Each of them callsa different method of our listbox. If we want to append a new item, we callthe Append() method. If we want to delete an item, we call the Delete()method. To clear all strings in a listbox, we call the Clear() method.#!/usr/bin/python# listbox.pyimport wxID_NEW = 1ID_RENAME = 2ID_CLEAR = 3ID_DELETE = 4class ListBox(wx.Frame):def __init__(self, parent, id, title):wx.Frame.__init__(self, parent, id, title, size=(350, 220))panel = wx.Panel(self, -1)hbox = wx.BoxSizer(wx.HORIZONTAL)self.listbox = wx.ListBox(panel, -1)hbox.Add(self.listbox, 1, wx.EXPAND | wx.ALL, 20)btnPanel = wx.Panel(panel, -1)vbox = wx.BoxSizer(wx.VERTICAL)new = wx.Button(btnPanel, ID_NEW, 'New', size=(90, 30))ren = wx.Button(btnPanel, ID_RENAME, 'Rename', size=(90, 30))dlt = wx.Button(btnPanel, ID_DELETE, 'Delete', size=(90, 30))clr = wx.Button(btnPanel, ID_CLEAR, 'Clear', size=(90, 30))self.Bind(wx.EVT_BUTTON, self.NewItem, id=ID_NEW)self.Bind(wx.EVT_BUTTON, self.OnRename, id=ID_RENAME)self.Bind(wx.EVT_BUTTON, self.OnDelete, id=ID_DELETE)self.Bind(wx.EVT_BUTTON, self.OnClear, id=ID_CLEAR)self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnRename)vbox.Add((-1, 20))vbox.Add(new)vbox.Add(ren, 0, wx.TOP, 5)vbox.Add(dlt, 0, wx.TOP, 5)vbox.Add(clr, 0, wx.TOP, 5)btnPanel.SetSizer(vbox)hbox.Add(btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20)panel.SetSizer(hbox)2 de 21 27/04/2008 1:05

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!