You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python和ttkbootstrap的Tableview获取选中行内容?

获取ttkbootstrap Tableview选中行内容

因为Tableview基于Tkinter Treeview实现,直接用Treeview的方法就能获取选中行数据,修改你的on_tree_select函数即可:

import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *

app = ttk.Window()
colors = app.style.colors

def on_tree_select(event):
    # 获取选中的行ID列表
    selected_rows = event.widget.selection()
    for row_id in selected_rows:
        # 提取行数据,values字段对应行内的所有值
        row_content = event.widget.item(row_id)['values']
        print(f"选中行内容: {row_content}")

coldata = [
    {"text": "LicenseNumber", "stretch": False},
    "CompanyName",
]

rowdata = [
    ('A123', 'IzzyCo'),
    ('A136', 'Kimdee Inc.')
]

dt = Tableview(
    master=app,
    coldata=coldata,
    rowdata=rowdata,
    paginated=True,
    searchable=True,
    bootstyle=PRIMARY,
    stripecolor=(colors.light, None),
)
dt.pack(fill=BOTH, expand=YES, padx=10, pady=10)
dt.bind('<<TreeviewSelect>>', on_tree_select)  # 用bind替代bind_all,避免全局事件干扰
app.mainloop()

关键说明:

  • event.widget指向触发事件的Treeview组件(即Tableview内部的tree对象)
  • selection()返回选中行的ID列表,支持处理多选场景
  • item(row_id)['values']会返回该行的完整数据,和你传入的rowdata元组内容一一对应

内容的提问来源于stack exchange,提问作者Rob McCormack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 00:02:19