【问题标题】:How to imitate this table using Tkinter?如何使用 Tkinter 模仿这张桌子?
【发布时间】:2014-04-22 18:44:37
【问题描述】:

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

【问题讨论】:

标签: python-3.x tkinter


【解决方案1】:

使用 Ttk/Tkinter Treeview 小部件。这提供了 tree-style 布局或带有标题布局的 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 上产生类似的结果:

【讨论】:

  • 第0列的宽度可以设置吗?
  • 是的。就像其他列一样。
【解决方案2】:

您必须创建一个 ext 条目数组,然后在父框架中使用“网格”布局管理器进行布局。

开发一个 Python 类以允许将网格和单元格内容作为单个表格进行管理, 实现诸如__getindex__ 之类的东西来获取单元格内容,甚至是一些响应式编程,允许某些列随着其他地方的值的变化而变化,这将是这样一个项目中有趣的部分。

要创建网格,只需:

import tkinter
window = tkinter.Tk()
frame = Tkinter.Frame(window)
frame.pack()

entries = {} # this 'entries'is what you might want to specify a custom class to manage
             # for now,a dictionary will do

for j in range(10):
    for i in range(10):
        e = tkinter.Entry(f)
        e.grid(column=i,row=j, borderwidth=0)
        es[i,j] = e

你来了。

【讨论】:

  • Entry 中的f 是什么?还有 es == 条目??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2014-10-14
相关资源
最近更新 更多