I’m designing a comboctrl using the ultimatelistctrl to show radiobuttons as options. But when I hover over the radio buttons their background is highlighted permanently in blue color. I have attached the image to show the exactly what is happening.
Below is the code:
#!/usr/bin/env python
import wx
from wx.lib.agw import ultimatelistctrl as ULC
import os
class FDListCtrl(ULC.UltimateListCtrl):
def __init__(self, parent, ID, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
agw_style = ULC.ULC_REPORT|ULC.ULC_NO_HEADER|ULC.ULC_HAS_VARIABLE_ROW_HEIGHT
ULC.UltimateListCtrl.__init__(self, parent, ID, pos, size, style, agwStyle=agw_style)
self.InsertColumn(0, '')
self.SetColumnWidth(0, -3)
class MyRadioBtn(wx.RadioButton):
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# 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):
radio = MyRadioBtn(self.lc, self.lc.GetItemCount(),
txt,
wx.DefaultPosition,
wx.DefaultSize)
index = self.lc.InsertStringItem(self.lc.GetItemCount(), "")
self.lc.SetItemWindow(index, 0, radio, expand=True)
# 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 = FDListCtrl(parent, 1, size=(1000, 1000))
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)
#----------------------------------------------------------------------
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
comboCtrl = wx.ComboCtrl(self, wx.ID_ANY, "", (20,20))
popupCtrl = ListCtrlComboPopup()
# It is important to call SetPopupControl() as soon as possible
comboCtrl.SetPopupControl(popupCtrl)
# Populate using wx.ListView methods
popupCtrl.AddItem("First Item")
popupCtrl.AddItem("Second Item")
popupCtrl.AddItem("Third Item")
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title=wx.GetApp().GetAppName())
self.CreateStatusBar(1)
sMsg = 'wxPython ' + wx.version()
self.SetStatusText(sMsg)
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
print('Running wxPython ' + wx.version())
app = wx.App(redirect=False)
app.SetAppName('Dropdown Search')
frame = MyFrame()
app.SetTopWindow(frame)
app.MainLoop()
@Robin please help me to overcome this situation