9

image

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

1

3 Answers 3

34

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
1

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.

1
  • What is f in the Entry? And es == entries??
    – Sid
    Commented Jun 21, 2020 at 14:00
-2

def karra_table(n): for i in range(1, 11): for j in range(1, n+1): print(f'{j:2d} * {i:2d} = {i*j:2d}', end='\t') print()

karra_table(int(input('Enter n: ')))

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 30, 2022 at 8:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.