【问题标题】:What is the best way to show data in a table in Tkinter?在 Tkinter 的表格中显示数据的最佳方式是什么?
【发布时间】:2018-05-31 13:18:10
【问题描述】:

我编写了一个程序,它从文本文件中获取数据并以表格样式格式显示。

来自文本文件的数据:

Jim,0.33
Dave,0.67
James,0.67
Eden,0.5

使用程序格式化:

Position | Name              |Score
-----------------------------------
1        |Dave               |0.67
2        |James              |0.67
3        |Eden               |0.5
4        |Jim                |0.33

如果不导入 Pandas / SQL 等,有没有更好的方法来显示这些数据?

我写的代码如下:

from tkinter import *

def show():

    tempList= [['Jim', '0.33'], ['Dave', '0.67'], ['James', '0.67'], ['Eden', '0.5']]

    tempList.sort(key=lambda e: e[1], reverse=True)
    listBox.insert(END, "Position | Name      \t\t |Score\n")
    listBox.insert(END,"-----------------------------------")
    listBox.insert(END,"\n")

    for i in range(len(tempList)):
        listBox.insert(END,(i+1))
        listBox.insert(END,"\t |")
        listBox.insert(END,tempList[i][0])
        listBox.insert(END,"\t \t|")
        listBox.insert(END,tempList[i][1])
        listBox.insert(END,"\n")

scores = Tk() 
label = Label(scores, text="High Scores", font = ("Arial",30)).grid(row = 0, columnspan = 3)
listBox= Text(scores,width = 40)
listBox.grid(row = 1,column= 0, columnspan = 2)
showScores = Button(scores, text = "Show scores",width = 15, command = show).grid(row = 4, column = 0)
closeButton = Button(scores, text = "Close",width = 15, command = exit).grid(row = 4, column = 1)

scores.mainloop()

【问题讨论】:

  • 您可以使用一系列框架和标签来显示数据。
  • 您也可以使用正确配置的 Treeview 来显示您的表格数据。

标签: python tkinter tabular


【解决方案1】:

不带树部分的ttk.Treeview 可用于显示表格:

tree = ttk.Treeview(master, columns=('Position', 'Name', 'Score'), show='headings')

然后用

设置列标签
tree.heading(<column>, text="Label")

并添加行

tree.insert("", "end", values=(<position>, <name>, <score>))

第一个参数是项的父项,因为你想要一个表,所有项都具有相同的父项,即根""。第二个参数是新项目在树中的位置。

完整示例:

import tkinter as tk
from tkinter import ttk

def show():

    tempList = [['Jim', '0.33'], ['Dave', '0.67'], ['James', '0.67'], ['Eden', '0.5']]
    tempList.sort(key=lambda e: e[1], reverse=True)

    for i, (name, score) in enumerate(tempList, start=1):
        listBox.insert("", "end", values=(i, name, score))

scores = tk.Tk() 
label = tk.Label(scores, text="High Scores", font=("Arial",30)).grid(row=0, columnspan=3)
# create Treeview with 3 columns
cols = ('Position', 'Name', 'Score')
listBox = ttk.Treeview(scores, columns=cols, show='headings')
# set column headings
for col in cols:
    listBox.heading(col, text=col)    
listBox.grid(row=1, column=0, columnspan=2)

showScores = tk.Button(scores, text="Show scores", width=15, command=show).grid(row=4, column=0)
closeButton = tk.Button(scores, text="Close", width=15, command=exit).grid(row=4, column=1)

scores.mainloop()

您可以找到有关Treeview 小部件here 的更多详细信息。

【讨论】:

    【解决方案2】:
    from tkinter import *
    from pandastable import Table
    
    root = Tk()
    frame = Frame(root)
    frame.pack()
    pt = Table(frame)
    pt.show()
    root.mainloop()
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多