【问题标题】:How to get the data from selected row in tkinter?如何从 tkinter 中的选定行中获取数据?
【发布时间】:2020-09-20 11:12:28
【问题描述】:

我在 tkinter 中创建了树视图,列代码在这里

        self.treeview = ttk.Treeview(self.treeviewFrame)
        self.treeview['columns'] = ('1','2','3','4','5','6','7')

        self.treeview.column('#0',width=50,minwidth=25)
        self.treeview.column('1',width=50,minwidth=25,anchor=W)
        self.treeview.column('2',width=50,minwidth=25,anchor=W)
        self.treeview.column('3',width=50,minwidth=25,anchor=W)
        self.treeview.column('4',width=50,minwidth=25,anchor=W)
        self.treeview.column('5',width=50,minwidth=25,anchor=W)
        self.treeview.column('6',width=50,minwidth=25,anchor=W)
        self.treeview.column('7',width=50,minwidth=25,anchor=W)


        self.treeview.bind('<ButtonRelease-1>',self.selectedRow)

        self.treeview.pack(fill=BOTH,expand=1)
        self.treeViewData()

为了检索选定的行,我使用了以下代码

self.treeview.bind('<ButtonRelease-1>',self.selectedRow)

def selectedRow(self,a):
        self.curItem = self.treeview.focus()
        self.contents = self.treeview.item(self.curItem)
        print(self.contents['values'])

在打印内容[值] 时,我命名为 S.No 的第一行 #0 没有打印出来。有没有办法从#0检索这些值

有八列,但只打印了 7 个值。第一行不打印。请帮帮我!

【问题讨论】:

  • 你还能包含更多代码吗?也许像一个例子,所以我们可以运行它
  • @CoolCloud 已添加!
  • 第 0 列是默认的,通常由选项文本填充。所以我猜#0 不是一个值。
  • 尝试在此行之前添加print(self.contents['text']) print(self.contents['values']) 并说出它是否出现。
  • @Atlas435 是的,它成功了!谢谢!你帮了我很多!!!!

标签: python user-interface tkinter treeview


【解决方案1】:

#0 列是默认的,通常由选项文本填充。所以我猜#0 不是一个值。

尝试在此行之前添加print(self.contents['text']) print(self.contents['values']) 并查看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多