How to create a combo box (Phoenix)

Keywords : Combo box.


Introduction :

wx.ComboBox is a combination of a single line text field, a button with a down arrow image and a listbox.

When you press the button, a listbox appears.

User can select only one option from the supplied string list.

wx.ComboBox has the following constructor :

wx.ComboBox(int id, string value='', wx.Point pos=wx.DefaultPosition, wx.Size size=wx.DefaultSize,
            wx.List choices=wx.EmptyList, int style=0, wx.Validator validator=wx.DefaultValidator,
            string name=wx.ComboBoxNameStr)

wx.ComboBox styles :

wx.ComboBox methods

string GetValue()

return the current value

SetValue(string value)

set the value into the textfield of the combobox

Copy()

copy the selected value to the clipboard

Cut()

cut the selected value to the clipboard

Paste()

paste text from the clipboard to the combobox's text field

SetInsertionPoint(int pos)

set the insertion point in the combobox's text field

int GetInsertionPoint()

get the insertion point for the combobox's text field

int GetLastPosition()

return the last position in the combobox's text field

Replace(int from, int to, string value)

replace the text between from and to positions in the combobox's text field

SetSelection(int n)

select the item at position n

SetMark(int from, int to)

select the text between from and to positions in the combobox's text field

(int from, int to) GetMark()

return the from and to positions of the selected text in the combobox's text field

int GetCurrentSelection()

return the current selection

bool SetStringSelection(string string)

select the item with the specified string

int SetString(int n, string string)

set the label for the item at position n

bool SetEditable(bool editable)

toggle readonly flag for the combobox's text field

int SetInsertionPointEnd()

set the insertion point at the end of the combobox's text field.

Remove(int from, int to)

remove the text between the two positions in the combobox's text field

bool IsEditable()

return true if the combobox is editable

SelectAll(int from, int to)

select all the text in the combo's text field

(info by Jan Bodnar / Zetcode).


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

   1 # sample_one.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 import random
  12 
  13 # class MyDialog
  14 # class MyApp
  15 
  16 #---------------------------------------------------------------------------
  17 
  18 class MyDialog(wx.Dialog):
  19     def __init__(self, parent, id, title):
  20         wx.Dialog.__init__(self, parent, id, title, size=(260, 300))
  21 
  22         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  23 
  24         #------------
  25 
  26         panel = wx.Panel(self, -1, (75, 20), (100, 127),  style=wx.SUNKEN_BORDER)
  27 
  28         self.picture = wx.StaticBitmap(panel)
  29         panel.SetBackgroundColour(wx.WHITE)
  30 
  31         self.images = ['tolstoy.jpg', 'feuchtwanger.jpg', 'balzac.jpg',
  32                        'pasternak.jpg', 'galsworthy.jpg', 'wolfe.jpg', 'zweig.jpg']
  33 
  34         authors = ['Leo Tolstoy', 'Lion Feuchtwanger', 'Honore de Balzac',
  35                    'Boris Pasternak', 'John Galsworthy', 'Tom Wolfe', 'Stefan Zweig' ]
  36 
  37         wx.ComboBox(self, -1, pos=(50, 170), size=(150, -1), choices=authors, style=wx.CB_READONLY)
  38 
  39         wx.Button(self, 1, '&Close', (80, 220))
  40 
  41         #------------
  42 
  43         self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
  44         self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
  45 
  46         #------------
  47 
  48         self.Centre()
  49 
  50     #-----------------------------------------------------------------------
  51 
  52     def OnClose(self, event):
  53         self.Close()
  54 
  55 
  56     def OnSelect(self, event):
  57         item = event.GetSelection()
  58         self.picture.SetFocus()
  59         self.picture.SetBitmap(wx.Bitmap('bitmaps/' + self.images[item]))
  60 
  61 #---------------------------------------------------------------------------
  62 
  63 class MyApp(wx.App):
  64     def OnInit(self):
  65         dlg = MyDialog(None, -1, 'wx.ComboBox')
  66         dlg.ShowModal()
  67         dlg.Destroy()
  68 
  69         return True
  70 
  71 #---------------------------------------------------------------------------
  72 
  73 app = MyApp(0)
  74 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 :

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


Comments

- blah, blah, blah....

How to create a combo box (Phoenix) (last edited 2020-12-30 17:45:34 by Ecco)

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