How to create a list box (Phoenix)

Keywords : List box.


Introduction :

wx.Listbox is a widget that consists of a scrolling box and a list of items.

User can select one or more items from that list

It depends on whether it is created as a single or multiple selection box.

Selected items are marked.

(info by ZetCode / Jan Bodnar).


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

This example consists of four different widgets :

- wx.Listbox,

- wx.TextCtrl,

- wx.StaticText,

- wx.Button.

Widgets are organized with wx.BoxSizer.

wx.Listbox has a list of six different world times.

These abbreviations are explained in the wx.TextCtrl.

Current time is displayed in the wx.StaticText widget.

wx.Timer widget is used to update the time every 100 miliseconds.

   1 # sample_one.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 from time import *
  12 
  13 # class MyFrame
  14 # class MyApp
  15 
  16 #---------------------------------------------------------------------------
  17 
  18 class MyFrame(wx.Frame):
  19     def __init__(self, parent, id, title):
  20         wx.Frame.__init__(self, parent, id, title,
  21                           wx.DefaultPosition, (430, 300))
  22 
  23         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  24         self.SetMinSize((430, 300))
  25 
  26         #------------
  27 
  28         zone_list = ['CET', 'GMT', 'MSK', 'EST', 'PST', 'EDT']
  29 
  30         self.full_list = {
  31             'CET': 'Central European Time',
  32             'GMT': 'Greenwich Mean Time',
  33             'MSK': 'Moscow Time',
  34             'EST': 'Eastern Standard Time',
  35             'PST': 'Pacific Standard Time',
  36             'EDT': 'Eastern Daylight Time'
  37         }
  38 
  39         self.time_diff = {
  40             'CET' : 1,
  41             'GMT' : 0,
  42             'MSK': 3,
  43             'EST': -5,
  44             'PST': -8,
  45             'EDT': -4
  46         }
  47 
  48         #------------
  49 
  50         self.diff = 0
  51         self.timer = wx.Timer(self, 1)
  52         self.timer.Start(100)
  53 
  54         #------------
  55 
  56         # wx.Font(pointSize, family, style, weight, underline, faceName)
  57         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  58         font.SetWeight(wx.BOLD)
  59 
  60         #------------
  61 
  62         panel = wx.Panel(self, -1)
  63 
  64         self.time_zones = wx.ListBox(panel, 26, wx.DefaultPosition, (180, 130), zone_list, wx.LB_SINGLE)
  65         self.time_zones.SetSelection(0)
  66         self.time_zones.SetFont(font)
  67         self.time_zones.SetForegroundColour('Black')
  68         self.time_zones.SetBackgroundColour('#cce8ff')
  69 
  70         self.text = wx.TextCtrl(panel, -1, 'Central European Time', size=(180, 130), style=wx.TE_MULTILINE)
  71         self.text.SetFont(font)
  72         self.text.SetForegroundColour('Black')
  73         self.text.SetBackgroundColour('#cce8ff')
  74 
  75         self.time = wx.StaticText(panel, -1, '', (-1, -1), (100, -1), wx.ALIGN_CENTER)
  76         self.time.SetFont(font)
  77         self.time.SetForegroundColour('#0078d7')
  78 
  79         btn = wx.Button(panel, wx.ID_CLOSE, '&Close')
  80 
  81         #------------
  82 
  83         vbox = wx.BoxSizer(wx.VERTICAL)
  84         hbox1 = wx.BoxSizer(wx.HORIZONTAL)
  85         hbox2 = wx.BoxSizer(wx.HORIZONTAL)
  86         hbox3 = wx.BoxSizer(wx.HORIZONTAL)
  87 
  88         hbox1.Add(self.time_zones, 0, wx.TOP, 40)
  89         hbox1.Add(self.text, 1, wx.LEFT | wx.TOP, 40)
  90         hbox2.Add(self.time, 1, wx.ALIGN_CENTRE)
  91         hbox3.Add(btn, 0, wx.ALIGN_CENTRE)
  92 
  93         vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
  94         vbox.Add(hbox2, 1, wx.ALIGN_CENTRE)
  95         vbox.Add(hbox3, 1, wx.ALIGN_CENTRE)
  96 
  97         panel.SetSizer(vbox)
  98 
  99         #------------
 100 
 101         self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
 102         self.Bind(wx.EVT_LISTBOX, self.OnSelect, id=26)
 103         self.Bind(wx.EVT_TIMER, self.OnTimer, id=1)
 104 
 105     #-----------------------------------------------------------------------
 106 
 107     def OnClose(self, event):
 108         self.Close()
 109 
 110 
 111     def OnSelect(self, event):
 112         index = event.GetSelection()
 113         time_zone = self.time_zones.GetString(index)
 114         self.diff = self.time_diff[time_zone]
 115         self.text.SetValue(self.full_list[time_zone])
 116 
 117 
 118     def OnTimer(self, event):
 119         ct = gmtime()
 120         print_time = (ct[0], ct[1], ct[2], ct[3]+self.diff, ct[4], ct[5], ct[6], ct[7], -1)
 121         self.time.SetLabel(strftime("%H:%M:%S", print_time))
 122 
 123 #---------------------------------------------------------------------------
 124 
 125 class MyApp(wx.App):
 126     def OnInit(self):
 127         frame = MyFrame(None, -1, 'wx.ListBox')
 128         frame.Centre()
 129         frame.Show(True)
 130 
 131         return True
 132 
 133 #---------------------------------------------------------------------------
 134 
 135 app = MyApp(0)
 136 app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

27/12/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a list box (Phoenix) (last edited 2020-12-31 16:17:52 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.