A combo control is a generic combobox that allows totally custom popup.
In addition it has other customization features. For instance, position and size of the dropdown button can be changed.
wx.ComboCtrl needs to be told somehow which control to use and this is done by SetPopupControl
. However, we need something more than just a wx.Control in this method as, for example, we need to call SetStringValue(“initial text value”) and wx.Control doesn’t have such method. So we also need a wx.ComboPopup which is an interface which must be implemented by a control to be usable as a popup. We couldn’t derive wx.ComboPopup from wx.Control as this would make it impossible to have a class deriving from a wxWidgets control and from it, so instead it is just a mix-in. Here’s a minimal sample of wx.ListView popup:
"""
A simple test case for wx.ComboCtrl using a wx.ListCtrl for the popup
"""
import wx
#----------------------------------------------------------------------
# This class is used to provide an interface between a ComboCtrl and the
# ListCtrl that is used as the popup for the combo widget.
class ListCtrlComboPopup(wx.ComboPopup):
def __init__(self):
wx.ComboPopup.__init__(self)
self.lc = None
def AddItem(self, txt):
self.lc.InsertItem(self.lc.GetItemCount(), txt)
def OnMotion(self, evt):
item, flags = self.lc.HitTest(evt.GetPosition())
if item >= 0:
self.lc.Select(item)
self.curitem = item
def OnLeftDown(self, evt):
self.value = self.curitem
self.Dismiss()
# The following methods are those that are overridable from the
# ComboPopup base class. Most of them are not required, but all
# are shown here for demonstration purposes.
# This is called immediately after construction finishes. You can
# use self.GetCombo if needed to get to the ComboCtrl instance.
def Init(self):
self.value = -1
self.curitem = -1
# Create the popup child control. Return true for success.
def Create(self, parent):
self.lc = wx.ListCtrl(parent, style=wx.LC_LIST | wx.LC_SINGLE_SEL | wx.SIMPLE_BORDER)
self.lc.Bind(wx.EVT_MOTION, self.OnMotion)
self.lc.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
return True
# Return the widget that is to be used for the popup
def GetControl(self):
return self.lc
# Called just prior to displaying the popup, you can use it to
# 'select' the current item.
def SetStringValue(self, val):
idx = self.lc.FindItem(-1, val)
if idx != wx.NOT_FOUND:
self.lc.Select(idx)
# Return a string representation of the current item.
def GetStringValue(self):
if self.value >= 0:
return self.lc.GetItemText(self.value)
return ""
# Called immediately after the popup is shown
def OnPopup(self):
wx.ComboPopup.OnPopup(self)
# Called when popup is dismissed
def OnDismiss(self):
wx.ComboPopup.OnDismiss(self)
# This is called to custom paint in the combo control itself
# (ie. not the popup). Default implementation draws value as
# string.
def PaintComboControl(self, dc, rect):
wx.ComboPopup.PaintComboControl(self, dc, rect)
# Receives key events from the parent ComboCtrl. Events not
# handled should be skipped, as usual.
def OnComboKeyEvent(self, event):
wx.ComboPopup.OnComboKeyEvent(self, event)
# Implement if you need to support special action when user
# double-clicks on the parent wxComboCtrl.
def OnComboDoubleClick(self):
wx.ComboPopup.OnComboDoubleClick(self)
# Return final size of popup. Called on every popup, just prior to OnPopup.
# minWidth = preferred minimum width for window
# prefHeight = preferred height. Only applies if > 0,
# maxHeight = max height for window, as limited by screen size
# and should only be rounded down, if necessary.
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return wx.ComboPopup.GetAdjustedSize(self, minWidth, prefHeight, maxHeight)
# Return true if you want delay the call to Create until the popup
# is shown for the first time. It is more efficient, but note that
# it is often more convenient to have the control created
# immediately.
# Default returns false.
def LazyCreate(self):
return wx.ComboPopup.LazyCreate(self)
Here’s how you would create and populate it in a dialog constructor:
comboCtrl = wx.ComboCtrl(self, wx.ID_ANY, "")
popupCtrl = ListViewComboPopup()
# It is important to call SetPopupControl() as soon as possible
comboCtrl.SetPopupControl(popupCtrl)
# Populate using wx.ListView methods
popupCtrl.InsertItem(popupCtrl.GetItemCount(), "First Item")
popupCtrl.InsertItem(popupCtrl.GetItemCount(), "Second Item")
popupCtrl.InsertItem(popupCtrl.GetItemCount(), "Third Item")
^^ This class supports the following styles:
wx.CB_READONLY
: Text will not be editable.
wx.CB_SORT
: Sorts the entries in the list alphabetically.
wx.TE_PROCESS_ENTER
: The control will generate the event wxEVT_TEXT_ENTER
(otherwise pressing Enter key is either processed internally by the control or used for navigation between dialog controls). Windows only.
wx.CC_SPECIAL_DCLICK
: Double-clicking triggers a call to popup’s OnComboDoubleClick. Actual behaviour is defined by a derived class. For instance, wx.adv.OwnerDrawnComboBox will cycle an item. This style only applies if wx.CB_READONLY
is used as well.
wx.CC_STD_BUTTON
: Drop button will behave more like a standard push button. ^^
^^
Handlers bound for the following event types will receive one of the wx.CommandEvent parameters.
EVT_TEXT: Process a wxEVT_TEXT
event, when the text changes.
EVT_TEXT_ENTER: Process a wxEVT_TEXT_ENTER
event, when RETURN
is pressed in the combo control.
EVT_COMBOBOX_DROPDOWN: Process a wxEVT_COMBOBOX_DROPDOWN
event, which is generated when the popup window is shown (drops down).
EVT_COMBOBOX_CLOSEUP: Process a wxEVT_COMBOBOX_CLOSEUP
event, which is generated when the popup window of the combo control disappears (closes up). You should avoid adding or deleting items in this event. ^^
wx.adv.OwnerDrawnComboBox, wx.richtext.RichTextStyleComboCtrl
Default constructor. |
|
This member function is not normally called in application code. |
|
Copies the selected text to the clipboard. |
|
Creates the combo control for two-step construction. |
|
Copies the selected text to the clipboard and removes the selection. |
|
Dismisses the popup window. |
|
This member function is not normally called in application code. |
|
This member function is not normally called in application code. |
|
Enables or disables popup animation, if any, depending on the value of the argument. |
|
Returns disabled button bitmap that has been set with |
|
Returns button mouse hover bitmap that has been set with |
|
Returns default button bitmap that has been set with |
|
Returns depressed button bitmap that has been set with |
|
Returns current size of the dropdown button. |
|
Returns custom painted area in control. |
|
Returns features supported by wx.ComboCtrl. |
|
Returns the current hint string. |
|
Returns the insertion point for the combo control’s text field. |
|
Returns the last position in the combo control text field. |
|
Returns the margins used by the control. |
|
Returns current popup interface that has been set with |
|
Returns popup window containing the popup control. |
|
Get the text control which is part of the combo control. |
|
Returns area covered by the text field (includes everything except borders and the dropdown button). |
|
Returns text representation of the current value. |
|
Dismisses the popup window. |
|
Returns |
|
Returns |
|
Returns |
|
Implement in a derived class to define what happens on dropdown button click. |
|
Pastes text from the clipboard to the text field. |
|
Shows the popup portion of the combo control. |
|
Prepare background of combo control or an item in a dropdown list in a way typical on platform. |
|
Removes the text between the two positions in the combo control text field. |
|
Replaces the text between two positions with the given text, in the combo control text field. |
|
Sets custom dropdown button graphics. |
|
Sets size and position of dropdown button. |
|
Set width, in pixels, of custom painted area in control without |
|
Sets a hint shown in an empty unfocused combo control. |
|
Sets the insertion point in the text field. |
|
Sets the insertion point at the end of the combo control text field. |
|
Uses the given window instead of the default text control as the main window of the combo control. |
|
Attempts to set the control margins. |
|
Set side of the control to which the popup will align itself. |
|
Set popup interface class derived from wx.ComboPopup. |
|
Extends popup size horizontally, relative to the edges of the combo control. |
|
Sets preferred maximum height of the popup. |
|
Sets minimum width of the popup. |
|
Selects the text between the two positions, in the combo control text field. |
|
Sets the text for the text field without affecting the popup. |
|
Set a custom window style for the embedded wx.TextCtrl. |
|
Sets the text for the combo control text field. |
|
Changes value of the control as if user had done it by selecting an item from a combo box drop-down list. |
|
Returns |
|
Show the popup. |
|
Undoes the last edit in the text field. |
|
Enable or disable usage of an alternative popup window, which guarantees ability to focus the popup control, and allows common native controls to function normally. |
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
wx.
ComboCtrl
(Control, TextEntry)¶Possible constructors:
ComboCtrl()
ComboCtrl(parent, id=ID_ANY, value="", pos=DefaultPosition,
size=DefaultSize, style=0, validator=DefaultValidator,
name=ComboBoxNameStr)
A combo control is a generic combobox that allows totally custom popup.
__init__
(self, *args, **kw)¶__init__ (self)
Default constructor.
__init__ (self, parent, id=ID_ANY, value=””, pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=ComboBoxNameStr)
Constructor, creating and showing a combo control.
parent (wx.Window) – Parent window. Must not be None
.
id (wx.WindowID) – Window identifier. The value wx.ID_ANY
indicates a default value.
value (string) – Initial selection string. An empty string indicates no selection.
pos (wx.Point) – Window position. If wx.DefaultPosition
is specified then a default position is chosen.
size (wx.Size) – Window size. If wx.DefaultSize
is specified then the window is sized appropriately.
style (long) – Window style. See wx.ComboCtrl.
validator (wx.Validator) – Window validator.
name (string) – Window name.
AnimateShow
(self, rect, flags)¶This member function is not normally called in application code.
Instead, it can be implemented in a derived class to create a custom popup animation.
The parameters are the same as those for DoShowPopup
.
rect (wx.Rect) –
flags (int) –
bool
True
if animation finishes before the function returns, False
otherwise. In the latter case you need to manually call DoShowPopup
after the animation ends.
Copy
(self)¶Copies the selected text to the clipboard.
Create
(self, parent, id=ID_ANY, value="", pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=ComboBoxNameStr)¶Creates the combo control for two-step construction.
Derived classes should call or replace this function. See wx.ComboCtrl for further details.
parent (wx.Window) –
id (wx.WindowID) –
value (string) –
pos (wx.Point) –
size (wx.Size) –
style (long) –
validator (wx.Validator) –
name (string) –
bool
Cut
(self)¶Copies the selected text to the clipboard and removes the selection.
Dismiss
(self)¶Dismisses the popup window.
Notice that calling this function will generate a wxEVT_COMBOBOX_CLOSEUP
event.
New in version 2.9.2.
DoSetPopupControl
(self, popup)¶This member function is not normally called in application code.
Instead, it can be implemented in a derived class to return default wx.ComboPopup, in case popup is None
.
popup (wx.ComboPopup) –
Note
If you have implemented OnButtonClick
to do something else than show the popup, then DoSetPopupControl
must always set popup to None
.
DoShowPopup
(self, rect, flags)¶This member function is not normally called in application code.
Instead, it must be called in a derived class to make sure popup is properly shown after a popup animation has finished (but only if AnimateShow
did not finish the animation within its function scope).
rect (wx.Rect) – Position to show the popup window at, in screen coordinates.
flags (int) – Combination of any of the following: wx.ComboCtrl.ShowAbove
, and wx.ComboCtrl.CanDeferShow
.
EnablePopupAnimation
(self, enable=True)¶Enables or disables popup animation, if any, depending on the value of the argument.
enable (bool) –
GetBitmapDisabled
(self)¶Returns disabled button bitmap that has been set with SetButtonBitmaps
.
The disabled state bitmap.
GetBitmapHover
(self)¶Returns button mouse hover bitmap that has been set with SetButtonBitmaps
.
The mouse hover state bitmap.
GetBitmapNormal
(self)¶Returns default button bitmap that has been set with SetButtonBitmaps
.
The normal state bitmap.
GetBitmapPressed
(self)¶Returns depressed button bitmap that has been set with SetButtonBitmaps
.
The depressed state bitmap.
GetClassDefaultAttributes
(variant=WINDOW_VARIANT_NORMAL)¶variant (WindowVariant) –
GetCustomPaintWidth
(self)¶Returns custom painted area in control.
int
See also
GetFeatures
()¶Returns features supported by wx.ComboCtrl.
If needed feature is missing, you need to instead use GenericComboCtrl, which however may lack a native look and feel (but otherwise sports identical API).
int
Value returned is a combination of the flags defined in wx.ComboCtrlFeatures.
GetHint
(self)¶Returns the current hint string.
See SetHint
for more information about hints.
string
New in version 2.9.1.
GetInsertionPoint
(self)¶Returns the insertion point for the combo control’s text field.
long
Note
Under Windows, this function always returns 0 if the combo control doesn’t have the focus.
GetLastPosition
(self)¶Returns the last position in the combo control text field.
long
GetMargins
(self)¶Returns the margins used by the control.
The x
field of the returned point is the horizontal margin and the y
field is the vertical one.
New in version 2.9.1.
Note
If given margin cannot be accurately determined, its value will be set to -1.
See also
GetPopupControl
(self)¶Returns current popup interface that has been set with SetPopupControl
.
GetTextCtrl
(self)¶Get the text control which is part of the combo control.
GetTextRect
(self)¶Returns area covered by the text field (includes everything except borders and the dropdown button).
GetValue
(self)¶Returns text representation of the current value.
For writable combo control it always returns the value in the text field.
string
HidePopup
(self, generateEvent=False)¶Dismisses the popup window.
generateEvent (bool) – Set this to True
in order to generate wxEVT_COMBOBOX_CLOSEUP
event.
Deprecated
Use Dismiss
instead.
IsKeyPopupToggle
(self, event)¶Returns True
if given key combination should toggle the popup.
event (wx.KeyEvent) –
bool
IsPopupShown
(self)¶Returns True
if the popup is currently shown.
bool
IsPopupWindowState
(self, state)¶Returns True
if the popup window is in the given state.
Possible values are:
ComboCtrl.Hidden |
Popup window is hidden. |
ComboCtrl.Animating |
Popup window is being shown, but the popup animation has not yet finished. |
ComboCtrl.Visible |
Popup window is fully visible. |
state (int) –
bool
OnButtonClick
(self)¶Implement in a derived class to define what happens on dropdown button click.
Default action is to show the popup.
Note
If you implement this to do something else than show the popup, you must then also implement DoSetPopupControl
to always return None
.
Paste
(self)¶Pastes text from the clipboard to the text field.
Popup
(self)¶Shows the popup portion of the combo control.
Notice that calling this function will generate a wxEVT_COMBOBOX_DROPDOWN
event.
New in version 2.9.2.
PrepareBackground
(self, dc, rect, flags)¶Prepare background of combo control or an item in a dropdown list in a way typical on platform.
This includes painting the focus/disabled background and setting the clipping region.
Unless you plan to paint your own focus indicator, you should always call this in your wx.ComboPopup.PaintComboControl
implementation. In addition, it sets pen and text colour to what looks good and proper against the background.
flags: wx.RendererNative flags: wx.CONTROL_ISSUBMENU
: is drawing a list item instead of combo control wx.CONTROL_SELECTED
: list item is selected wx.CONTROL_DISABLED
: control/item is disabled
Remove
(self, frm, to)¶Removes the text between the two positions in the combo control text field.
frm (long) –
to (long) – The last position.
The first position.
Replace
(self, frm, to, text)¶Replaces the text between two positions with the given text, in the combo control text field.
frm (long) –
to (long) – The second position.
text (string) – The text to insert.
The first position.
SetButtonBitmaps
(self, bmpNormal, pushButtonBg=False, bmpPressed=BitmapBundle(), bmpHover=BitmapBundle(), bmpDisabled=BitmapBundle())¶Sets custom dropdown button graphics.
bmpNormal (wx.BitmapBundle) – Default button image.
pushButtonBg (bool) – If True
, blank push button background is painted below the image.
bmpPressed (wx.BitmapBundle) – Depressed button image.
bmpHover (wx.BitmapBundle) – Button image when mouse hovers above it. This should be ignored on platforms and themes that do not generally draw different kind of button on mouse hover.
bmpDisabled (wx.BitmapBundle) – Disabled button image.
SetButtonPosition
(self, width=-1, height=-1, side=RIGHT, spacingX=0)¶Sets size and position of dropdown button.
width (int) – Button width. Value = 0 specifies default.
height (int) – Button height. Value = 0 specifies default.
side (int) – Indicates which side the button will be placed. Value can be wx.LEFT
or wx.RIGHT
.
spacingX (int) – Horizontal spacing around the button. Default is 0.
SetCustomPaintWidth
(self, width)¶Set width, in pixels, of custom painted area in control without CB_READONLY
style.
In read-only wx.adv.OwnerDrawnComboBox, this is used to indicate area that is not covered by the focus rectangle.
width (int) –
SetHint
(self, hint)¶Sets a hint shown in an empty unfocused combo control.
Notice that hints are known as cue banners under MSW or placeholder strings under macOS.
hint (string) –
bool
New in version 2.9.1.
See also
SetInsertionPoint
(self, pos)¶Sets the insertion point in the text field.
pos (long) – The new insertion point.
SetInsertionPointEnd
(self)¶Sets the insertion point at the end of the combo control text field.
SetMainControl
(self, win)¶Uses the given window instead of the default text control as the main window of the combo control.
By default, combo controls without CB_READONLY
style create a wx.TextCtrl which shows the current value and allows to edit it. This method allows to use some other window instead of this wx.TextCtrl.
This method can be called after creating the combo fully, however in this case a wx.TextCtrl is unnecessarily created just to be immediately destroyed when it’s replaced by a custom window. If you wish to avoid this, you can use the following approach, also shown in the combo sample:
# Create the combo control using its default ctor.
combo = wx.ComboCtrl()
# Create the custom main control using its default ctor too.
someMainWindow = SomeWindow()
# Set the custom main control before creating the combo.
combo.SetMainControl(someMainWindow)
# And only create it now: wx.TextCtrl won't be unnecessarily
# created because the combo already has a main window.
combo.Create(panel, wx.ID_ANY, "")
# Finally create the main window itself, now that its parent was
# created.
someMainWindow.Create(combo, ...)
Note that when a custom main window is used, none of the methods of this class inherited from wx.TextEntry, such as SetValue
, Replace
, SetInsertionPoint
etc do anything and simply return.
win (wx.Window) –
New in version 4.1/wxWidgets-3.1.6.
SetMargins
(self, *args, **kw)¶Attempts to set the control margins.
When margins are given as wx.Point, x indicates the left and y the top margin. Use -1 to indicate that an existing value should be used.
True
if setting of all requested margins was successful.
New in version 2.9.1.
SetMargins (self, pt)
pt (wx.Point) –
bool
SetMargins (self, left, top=-1)
left (int) –
top (int) –
bool
SetPopupAnchor
(self, anchorSide)¶Set side of the control to which the popup will align itself.
Valid values are LEFT
, RIGHT
and 0. The default value 0 means that the most appropriate side is used (which, currently, is always LEFT
).
anchorSide (int) –
SetPopupControl
(self, popup)¶Set popup interface class derived from wx.ComboPopup.
This method should be called as soon as possible after the control has been created, unless OnButtonClick
has been overridden.
popup (wx.ComboPopup) –
SetPopupExtents
(self, extLeft, extRight)¶Extends popup size horizontally, relative to the edges of the combo control.
extLeft (int) – How many pixel to extend beyond the left edge of the control. Default is 0.
extRight (int) – How many pixel to extend beyond the right edge of the control. Default is 0.
Note
Popup minimum width may override arguments. It is up to the popup to fully take this into account.
SetPopupMaxHeight
(self, height)¶Sets preferred maximum height of the popup.
height (int) –
Note
Value -1 indicates the default.
SetPopupMinWidth
(self, width)¶Sets minimum width of the popup.
If wider than combo control, it will extend to the left.
width (int) –
Note
Value -1 indicates the default. Also, popup implementation may choose to ignore this.
SetSelection
(self, frm, to)¶Selects the text between the two positions, in the combo control text field.
frm (long) –
to (long) – The second position.
The first position.
SetText
(self, value)¶Sets the text for the text field without affecting the popup.
Thus, unlike SetValue
, it works equally well with combo control using CB_READONLY
style.
value (string) –
SetTextCtrlStyle
(self, style)¶Set a custom window style for the embedded wx.TextCtrl.
Usually you will need to use this during two-step creation, just before Create
. For example:
comboCtrl = wx.ComboCtrl()
# Let's make the text right-aligned
comboCtrl.SetTextCtrlStyle(wx.TE_RIGHT)
comboCtrl.Create(parent, wx.ID_ANY, "")
style (int) –
SetValue
(self, value)¶Sets the text for the combo control text field.
value (string) –
Note
For a combo control with CB_READONLY
style the string must be accepted by the popup (for instance, exist in the dropdown list), otherwise the call to SetValue
is ignored.
SetValueByUser
(self, value)¶Changes value of the control as if user had done it by selecting an item from a combo box drop-down list.
value (string) –
ShouldDrawFocus
(self)¶Returns True
if focus indicator should be drawn in the control.
bool
Undo
(self)¶Undoes the last edit in the text field.
Windows only.
UseAltPopupWindow
(self, enable=True)¶Enable or disable usage of an alternative popup window, which guarantees ability to focus the popup control, and allows common native controls to function normally.
This alternative popup window is usually a wx.Dialog, and as such, when it is shown, its parent top-level window will appear as if the focus has been lost from it.
enable (bool) –
BitmapDisabled
¶BitmapHover
¶See GetBitmapHover
BitmapNormal
¶See GetBitmapNormal
BitmapPressed
¶See GetBitmapPressed
ButtonSize
¶See GetButtonSize
CustomPaintWidth
¶InsertionPoint
¶See GetInsertionPoint
and SetInsertionPoint
LastPosition
¶See GetLastPosition
Margins
¶See GetMargins
and SetMargins
PopupControl
¶See GetPopupControl
and SetPopupControl
PopupWindow
¶See GetPopupWindow
TextCtrl
¶See GetTextCtrl
TextRect
¶See GetTextRect