Search code examples
pythontreeclickwxpythonwxwidgets

Avoid click to get out of wxPython TreeCtrl in a Notebook


Below is a very simple wxPython code creating a Notebook inside which are several panels containing TreeCtrl objects.

Using it, I get a behavior I would like to avoid:

When I click in a tree, then I cannot switch directly to another page of the notebook without clicking first outside the tree. This means that it needs two clicks to change the notebook page: One to get outside the tree, another to switch the page.

I would like to be able to do this in one single click.

The code:

import wx

class TestFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY)

        # Create the notebook
        notebook = wx.Notebook(self)

        # Put panels in the notebook
        notebook.AddPage(TestPanel(notebook), "Page 1")
        notebook.AddPage(TestPanel(notebook), "Page 2")

        # Display the window
        self.Show(True)


class TestPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # Create the sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        # Create the tree
        tree = wx.TreeCtrl(self)
        sizer.Add(tree, 1, wx.EXPAND)

        # Create nodes in the tree
        root = tree.AddRoot("root")

        tree.AppendItem(root, "item 1")
        tree.AppendItem(root, "item 2")
        tree.AppendItem(root, "item 3")

        # Expand the root node
        tree.Expand(root)


if __name__ == "__main__":

    # Create an application without redirection of stdout/stderr to a window
    application = wx.App(False)

    # Open a main window
    frame = TestFrame()

    # Launch the application
    application.MainLoop()

Solution

  • This looks like this bug which should be fixed in 3.0.2. If you're using an earlier version, please upgrade.