在 Tkinter 的表格中显示数据的最佳方式是什么? [英] What is the best way to show data in a table in Tkinter?

查看:44
本文介绍了在 Tkinter 的表格中显示数据的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a program which takes data from a text file and displays it in a table style format.

来自文本文件的数据:

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 等,是否有更好的方式来显示这些数据?

Without importing Pandas / SQL etc is there a better way of displaying this data?

我写的代码如下:

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()

推荐答案

一个没有树部分的ttk.Treeview可以用来显示表格:

A ttk.Treeview without the tree part can be used to display a table:

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

然后用

tree.heading(<column>, text="Label")

并添加行

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

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

The first argument is the item's parent, since you want a table, all items have the same parent, the root "". The second argument is the position of the new item in the tree.

完整示例:

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 小部件的更多详细信息 这里.

You can find more details about the Treeview widget here.

这篇关于在 Tkinter 的表格中显示数据的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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