Hi, Everyone,
I’ve got a problem on changing background color of wx.Checkbox widget. Note that this problem does not appear on Linux, but Windows doesn’t work.
The usage should be quite simple. I want eacy checkbox to change color when clicked (switched between true/false).
Firstly I created checkboxes with EVT_CHECKBOX event bound to a lambda so that when clicked, a hook will trigger, passing the checkbox object in:
def new_static(self, value, check_list=None):
"""Setup check_list to a list of checkboxes, so that the static will
change background color if any of the checkbox is selected"""
new = wx.StaticText(self, wx.ID_ANY, value)
self.core.items_others.append(new)
if check_list:
entry = [new, check_list]
self.core.dynamic_statics.append(entry)
return new
def new_checkbox(self, value):
new = wx.CheckBox(self, wx.ID_ANY, value, size=g_checkbox_size)
self.Bind(wx.EVT_CHECKBOX,
lambda event, x=new: self.core.do_update_checkbox(new),
new)
self.core.items_checkboxes.append(new)
return new
def new_button(self, value):
new = wx.Button(self, wx.ID_ANY, value)
self.core.items_buttons.append(new)
Then in the hook, I do:
item.SetBackgroundColour(g_color_disable)
def do_quit(self, event):
print("Quitting...")
self.quit_app()
def refresh(self):
# This is required for Windows
self.outter_frame.Refresh()
def do_update_checkbox(self, new):
self.color_set(new, new.GetValue())
self.refresh_static_texts()
def refresh_checkboxes(self):
for box in self.items_checkboxes:
self.do_update_checkbox(box)
self.refresh()
def refresh_static_texts(self):
for entry in self.dynamic_statics:
Which should call wx.Checkbox.SetBackgroundColor() so that the color of the checkbox will reflect its value (true/false).
This works perfect on Linux, however Windows didn’t work. I can see that the color flashed quickly but gone.
Anyone has suggestions on how to fix this on Windows? Please refer to the whole program as well if needed [1].
[1] https://github.com/xzpeter/pylibs/blob/bf45345dfa71ee69cb86e5e2d868f49f54c44e83/double-ball.py
Thanks!
Peter
Robin
October 19, 2020, 7:04pm
2
One of the downsides of using native widgets is that sometimes they ignore some of the common/generic methods inherited from wx.Window
, like SetBackgroundColour
. We can’t do anything about it when the platform decides that certain widgets should look a certain way and refuses to do what we ask them to do.
There is a generic checkbox class that was recently added for 4.1.1, which should have no problem setting the background color. You can get it from a snapshot build , or at https://github.com/wxWidgets/Phoenix/blob/6dd5689daa3f473f7d06c2f2c956dd3bcae4538d/wx/lib/checkbox.py
Hello, Robin,
The new wx.lib.chekbox.GenCheckBox worked for me!
Thanks for answering!
Best Regards,
Peter