Can I combine text entry and a list

The widget that combines text entry and a list is called a combo box, and is essentially a text box bolted to a pull-down choice. Figure 7.17 displays a combo box.

On Windows, you can use the right-hand style, which is a text box bolted to a list box.

The code for creating a combo box is similar to the choice elements we have already seen. In this case the class is wx.ComboBox, which is a direct subclass of wx.Choice. Listing 7.15 displays the code details.

Listing 7.15 Code for a demonstration of wx.ComboBox import wx class ComboBoxFrame(wx.Frame):

wx.Frame._init_(self, None, -1, 'Combo Box Example', size=(350, 300)) panel = wx.Panel(self, -1)

sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',

'six', 'seven', 'eight'] wx.StaticText(panel, -1, "Select one:", (15, 15))

wx.ComboBox(panel, -1, "default value", (15, 30), wx.DefaultSize, sampleList, wx.CB_DROPDOWN) wx.ComboBox(panel, -1, "default value", (150, 30), wx.DefaultSize, sampleList, wx.CB_SIMPLE)

if __name__ == '__main__': app = wx.PySimpleApp() ComboBoxFrame().Show() app.MainLoop()

—J Combo Box Example


Select one:

1 default value| v

default value

zero one two



five six

seven eight

Figure 7.17 A combo box showing the left box in the style wx.cb_dropdown, and the right in wx.cb_simple

The constructor for wx.ComboBox should look familiar by now:

wx.ComboBox(parent, id, value="", pos=wx.DefaultPosition, size=wx.DefaultSize, choices, style=0, validator=wx.DefaultValidator, name="comboBox")

There are four styles for a wx.ComboBox. Two of them determine how the combo box is drawn: wx.CB_DROPDOWN creates a combo box with a pull-down list, and wx.CB_SlMPLE creates a combo box with a full list box. You can only use wx.CB_SlMPLE on Windows systems. Any combo box can be designated wx.cb_readonly, which prevents the user from typing in the text area. When the combo box is designated read only, the selection must come from one of the elements in the choice list, even if you set it programmatically. Finally, there is wx.CB_SORT, which causes the elements of the choice list to be displayed alphabetically.

Since wx.ComboBox is a subclass of wx.Choice, all of the methods of wx.Choice can be called on a combo box, as displayed in table 7.14. In addition, a number of methods are defined to manipulate the text component, all of which behave the way they do for a wx.TextCtrl (see table 7.4 for details). The defined methods are Copy(), Cut(), GetInsertionPoint(), GetValue(), Paste(), Replace(from, to, text), Remove(from, to), SetlnsertionPoint(pos), SetInsertionPointEnd(), and SetValue()

Was this article helpful?

0 0


  • laura
    How to make the wxchoice list entries readable?
    8 years ago

Post a comment