下面是一个非常简单的wxPython代码,用于创建一个Notebook,其中包含几个包含TreeCtrl对象的面板。
使用它,我得到了一个我想避免的行为:
当我在树中单击时,我不能直接切换到笔记本的另一页而不先在树外单击。这意味着需要两次点击才能更改笔记本页面:一个用于到达树外,另一个用于切换页面。
我希望能够一键完成。
代码:
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()