I have a ListCtrl that can update itself with various items. To do that, I empty it, and than I append several items.
Then I want to catch the EVT_LIST_ITEM_FOCUSED event. On Windows, Unix and MacOS, it works fine.
Finally, I want to catch the event after updating my list. That happen automatically on Unix and MacOS, but it's not the case on Windows. That's why I would like to generate an event at the end of the "update()" method.
Example in code :
import wx
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None)
self.Show()
# Create the ListCtrl
self.list_ctrl = wx.ListCtrl(self, style=wx.LC_REPORT)
self.list_ctrl.AppendColumn("Column")
# Bind the event to the callback function
self.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_focus, self.list_ctrl)
# Fill the list with fruits
self.update(["apples", "bananas", "pears"])
def update(self, name_list):
"""Refill the ListCtrl with the content of the list."""
# Empty the ListCtrl
self.list_ctrl.DeleteAllItems()
# Refill it
for element in name_list:
self.list_ctrl.Append([element])
def on_focus(self, event):
"""Print what is currently focused."""
focused = event.GetItem().GetText()
if focused == "":
print("No focus.")
else:
print(f"{focused} is focused.")
app = wx.App()
main_frame = MainFrame()
app.MainLoop()
This code print "apples is focused" at the start of the program with both Unix and MacOS. On Windows, it print nothing, because the event is not triggered. What I want is getting the message "apples is focused" on Windows.
Constraints :
- I want to use an event, because I intend to
Skip()it to the Panel higher in the hierarchie. - I want to set this event with a Item Text of my choice, so the program can print "No focus" if there's no item in the ListCtrl. Thus calling
self.list_ctrl.Focus(0)doesn't work, as it does nothing when there are no item.
Thnks for your help and have a nice day.
Ok, so I found a way to work around the problem by getting rid of one constraint (I wanted to
Skip()the event if needed).Here's the code I use.
I you have cleaner ways to do it, I'm interested.