gpt4 book ai didi

python-3.x - 如何使用 Tkinter 模仿这个表?

转载 作者:行者123 更新时间:2023-12-02 22:35:00 25 4
gpt4 key购买 nike

image

如何开始使用 Tkinter 创建类似的表格?

最佳答案

使用 Ttk/Tkinter Treeview小部件。这提供了树形布局或带有标题布局的listview样式列。

由于 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 上产生类似这样的结果:

treeview demo

关于python-3.x - 如何使用 Tkinter 模仿这个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22456445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com