单击 Tkinter Treeview 小部件的项目的命令? [英] Command for clicking on the items of a Tkinter Treeview widget?

查看:27
本文介绍了单击 Tkinter Treeview 小部件的项目的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Tkinter 创建一个 GUI,GUI 的主要部分是两个 Treeview 对象.当一个项目(即目录)被点击两次时,我需要 Treeview 对象的内容来更改.

I'm creating a GUI with Tkinter, and a major part of the GUI is two Treeview objects. I need the contents of the Treeview objects to change when an item (i.e. a directory) is clicked twice.

如果 Treeview 项目是按钮,我只能将 command 设置为适当的功能.但是我找不到为 Treeview 项目创建 "on_click" 行为的方法.

If Treeview items were buttons, I'd just be able to set command to the appropriate function. But I'm having trouble finding a way to create "on_click" behavior for Treeview items.

什么 Treeview 选项、方法等使我能够将命令绑定到特定项目并执行该命令 "on_click"?

What Treeview option, method, etc, enables me to bind a command to particular items and execute that command "on_click"?

推荐答案

如果您希望在用户双击时发生某些事情,请为 "" 添加绑定.由于单击即可设置选择,因此在您的回调中,您可以查询小部件以找出选择的内容.例如:

If you want something to happen when the user double-clicks, add a binding to "<Double-1>". Since a single click sets the selection, in your callback you can query the widget to find out what is selected. For example:

import tkinter as tk
from tkinter import ttk

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.tree = ttk.Treeview()
        self.tree.pack()
        for i in range(10):
            self.tree.insert("", "end", text="Item %s" % i)
        self.tree.bind("<Double-1>", self.OnDoubleClick)
        self.root.mainloop()

    def OnDoubleClick(self, event):
        item = self.tree.selection()[0]
        print("you clicked on", self.tree.item(item,"text"))

if __name__ == "__main__":
    app = App()

这篇关于单击 Tkinter Treeview 小部件的项目的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆