I'm writing code for a some lasercutter software ( https://github.com/meerk40t/meerk40t ) and there's a wx.TreeCtrl in Multiple Selections mode using wxPython 4.0+. For some particular tree items, I want a selection to also select other items in the same tree simply as an indication that they are linked to the same data though they aren't focused. While this works fine in Windows, the code for selection event goes wrong for GTK, OSX stuff. When the other items are selected, it calls code akin to EnsureVisibile() and tries to scroll to those other selected items. This would be fine if I could turn it off, but I can't seem to turn it off. It scrolls to the other selected items making dragging and dropping impossible.
Is there someway to select an item without the SelectItem() code? Is there anyway to disable that functionality for OSX and Linux?
I tried nuking the EnsureVisible() function, selecting the items again selecting the focused item a second time to undo the scrolling and searched everywhere I could for a fix. Any ideas?
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((400, 300))
self.tree = wx.TreeCtrl(self, wx.ID_ANY, style=wx.TR_MULTIPLE)
self.root = self.tree.AddRoot("My Tree")
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.tree, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_tree_selection, self.tree)
for j in range(20):
for k in range(5):
self.tree.AppendItem(self.root, str(k))
self.tree.ExpandAll()
def on_tree_selection(self, event):
label = self.tree.GetItemText(self.tree.GetFocusedItem())
h = self.tree.GetFirstChild(self.root)[0]
while h.IsOk():
if label == self.tree.GetItemText(h):
self.tree.SelectItem(h, True)
h = self.tree.GetNextSibling(h)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
Basic App with the problem. In windows it selects all the stuff and works fine. In MacOSX or Linux or RPi it scrolls making the code unusable.
I agree that the newly selected item shouldn't be selected if it's not the only selected item, but unfortunately it doesn't seem to be possible to disable this behaviour from outside of the control currently. This was just fixed in wxWidgets itself, so you shouldn't have this problem if you use the next wxWidgets/wxPython release (or rebuild them from master sources right now), but in the meanwhile I can only recommend some other way of indicating that the items are linked because, IMHO, selecting them is not especially user-friendly in any case, as this is something that is supposed to be under user control anyhow. For example, you could change their font to be italic, or use some special colour or maybe even (temporarily) change their label.