How to imitate this table using Tkinter?

前端 未结 2 1741
闹比i
闹比i 2020-12-28 22:13

\"image\"

How to get started with creating a similar table using Tkinter?

相关标签:
2条回答
  • 2020-12-28 23:04

    Use the Ttk/Tkinter Treeview widget. This provides either a tree-style layout or a listview style columns with headings layout.

    As the Treeview widget is from Tk's themed icon set it will look appropriate on Windows - picking up the current border and column heading styles so that the look should match the current example posted.

    Example (that will work in both 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()
    

    This should yield something like this on Windows:

    treeview demo

    0 讨论(0)
  • 2020-12-28 23:09

    You have to create an array of ext entries, and lay then out with the "grid" layout manager in a parent frame.

    Developing a Python's class to allow managing the grid and cell contents as a single table, implementing things like __getindex__ to get cell contents, and even some bits of reactive programing, allowing certain cols to change with values changing elsewhere would be the fun part in such a project.

    To create the grid, it is just a matter of:

    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
    

    And there you are.

    0 讨论(0)
提交回复
热议问题