从 wxtreeCtrl 获取选定的项目

发布于 2024-12-12 08:20:15 字数 412 浏览 1 评论 0原文

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

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

在方法 OnAdd 中,我尝试获取该项目:

    def OnAdd(self, event):
        item =  event.GetItem()

但它给出错误,表明事件没有 GetItem() 方法。有什么想法吗?

更新:

我分配了一个按钮事件来处理所选项目。 这就是为什么该活动没有附加项目的原因。

How should you get the item selected in wxTreeCtrl? I bind the method to the activated item like this:

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

And in the method OnAdd I try to get the item:

    def OnAdd(self, event):
        item =  event.GetItem()

But it gives error that event has no GetItem() method. Any idea?

UPDATE:

I had assigned a button event to process selected item.
So that's why the event had not item attached to it..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-12-19 08:20:15

您错误地绑定了回调。您当前所做的:

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

但第三个参数是id 是第四个参数。因此,将其更改为:

self.tree = wx.TreeCtrl(self, size=(200,100)) # Or however you defined it
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnAdd, self.tree, id=10)

这样,您将在 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()

You are binding the callback incorrectly. You currently do:

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

But the 3rd parameter is the source; id is the 4th parameter. So, change it to this:

self.tree = wx.TreeCtrl(self, size=(200,100)) # Or however you defined it
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnAdd, self.tree, id=10)

This way, the event argument you will get in your OnAdd function will be the tree instance, which will have the GetItem method available.

Full example:

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()
最近可好 2024-12-19 08:20:15

只是我的 2 美分:

两天来我一直在寻找 C++/wxWidgets 的相同解决方案。

我发现了一个非常好的工作示例:

  1. 我将 Codeblocks 与 wxSmith(RAD 工具)一起使用。 Ubuntu Bionic

  2. 从 Windows 进行 Ssh &&出口展示&& Codeblocks

这是此特定事件的代码...

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 !


}

youtube 上的工作示例

just my 2cents:

I've been looking for the same solution with C++/wxWidgets for 2 days.

I found a really good working example:

  1. I use Codeblocks with wxSmith(RAD tool). Ubuntu Bionic

  2. Ssh from windows && export display && Codeblocks

And here's the code for this specific Event...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文