AttributeError:'str'对象在树视图项目中没有属性'x'

时间:2019-10-19 05:29:48

标签: tkinter

使用python 3.7

嗨。我试图获取选定的treeview项,并希望在单击左菜单项后将其打印出来。这是我的树状列表。当我右键单击菜单时,显示停止过程命令。我试图获取所选项目并打印它,但是它给我错误

AttributeError: 'str' object has no attribute 'x' in treeview item

这是我的树列表

enter image description here

这是我的代码

self.popup_menu.add_command(label="stop process",
                                    command=lambda:self.delete_selected("<Button-3>"))
        self.tree.bind('<Button-3>', self.popup)

    def delete_selected(self, event):
        item = self.tree.identify('name','ID',event.x, event.y)
        print(item)

    def popup(self, event):
        """action in event of button 3 on tree view"""

        try:
            self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
        finally:
            self.popup_menu.grab_release()

2 个答案:

答案 0 :(得分:0)

您正在将str传递给delete_selected,然后尝试读取它的xy属性。不要将"<Button-3>"字符串传递给方法。而是在lambda中接收event参数,并将其传递给delete_selected

self.popup_menu.add_command(label="stop process", command=lambda event:self.delete_selected(event))

或者只是删除lambda并直接传递函数:

self.popup_menu.add_command(label="stop process", command=self.delete_selected)

答案 1 :(得分:0)

这对我有用

self.popup_menu.add_command(label="stop process",
                                    command=self.delete_selected)

        self.tree.bind('<Button-3>', self.popup)



def delete_selected(self):

    try:
        curItem = self.tree.item(self.tree.focus())
        item = self.tree.selection()[0]
        self.kill_process(curItem['values'])
        self.tree.delete(item)

    except:
         pass