使用 Ttk/TkinterTreeview小部件。这提供了一个树型布局或listview带有标题布局的样式列。
As the Treeview
小部件来自 Tk 的主题图标集,它在 Windows 上看起来很合适 - 选择当前边框和列标题样式,以便外观应与当前发布的示例相匹配。
示例(适用于 Python 2 和 3):
try:
from Tkinter import *
from ttk import *
except ImportError: # Python 3
from tkinter import *
from tkinter.ttk import *
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.CreateUI()
self.LoadTable()
self.grid(sticky = (N,S,W,E))
parent.grid_rowconfigure(0, weight = 1)
parent.grid_columnconfigure(0, weight = 1)
def CreateUI(self):
tv = Treeview(self)
tv['columns'] = ('starttime', 'endtime', 'status')
tv.heading("#0", text='Sources', anchor='w')
tv.column("#0", anchor="w")
tv.heading('starttime', text='Start Time')
tv.column('starttime', anchor='center', width=100)
tv.heading('endtime', text='End Time')
tv.column('endtime', anchor='center', width=100)
tv.heading('status', text='Status')
tv.column('status', anchor='center', width=100)
tv.grid(sticky = (N,S,W,E))
self.treeview = tv
self.grid_rowconfigure(0, weight = 1)
self.grid_columnconfigure(0, weight = 1)
def LoadTable(self):
self.treeview.insert('', 'end', text="First", values=('10:00',
'10:10', 'Ok'))
def main():
root = Tk()
App(root)
root.mainloop()
if __name__ == '__main__':
main()
在 Windows 上这应该会产生类似这样的结果: