从wxtreeC获取所选项目

2024-05-16 03:36:22 发布

您现在位置:Python中文网/ 问答频道 /正文

您应该如何在wxTreeCtrl中选择项目?我将方法绑定到激活项,如下所示:

 self.tree.Bind (wx.EVT_TREE_ITEM_ACTIVATED, self.OnAdd, id=10)

OnAdd方法中,我试图得到项目:

^{pr2}$

但它给出了一个错误,即event没有GetItem()方法。有什么想法吗?在

更新:

我分配了一个按钮事件来处理所选项目。 所以这就是为什么这个活动没有附加物品。。在


Tags: 项目方法selfidtreebind错误item
2条回答

就我的二十几岁:

我一直在寻找与C++ /WxWiWes相同的解决方案,持续2天。在

我发现了一个很好的例子:

  1. 我在wxSmith(RAD工具)中使用代码块。Ubuntu仿生

  2. 来自windows的Ssh&导出显示和代码块

这是这个特定事件的代码。。。在

void test12052019Frame::OnTreeCtrl1ItemActivated(wxTreeEvent& event)
{
//TreeCtrl1 is my tree
//when I click on any option of my tree
//it activates a wxMessageBox with the label
//of the option selected...
//just let go your imagination :)
//A youtube video will follow.

wxString thelabel;
wxTreeItemId test3;

test3 = TreeCtrl1->GetSelection();//id of the item selected
thelabel = TreeCtrl1->GetItemText(test3);//extract associated text

wxMessageBox(thelabel); //shazam !


}

Working example on youtube

您没有正确绑定回调。您目前需要:

self.Bind (wx.EVT_TREE_ITEM_ACTIVATED, self.OnAdd, id=10)

但是第三个参数是sourceid是第四个参数。所以,把它改成:

^{pr2}$

这样,您将在OnAdd函数中得到的event参数将是tree实例,该实例具有可用的GetItem方法。在

完整示例:

import wx

class TreeExample(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Tree Example', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))

        root = self.tree.AddRoot('root')
        for item in ['item1', 'item2', 'item3']:
            self.tree.AppendItem(root, item)
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree)
        self.tree.Expand(root)

    def OnActivated(self, evt):
        print 'Double clicked on', self.tree.GetItemText(evt.GetItem())

app = wx.PySimpleApp(None)
TreeExample().Show()
app.MainLoop()

相关问题 更多 >