Does Tkinter Have a Table Widget

Does tkinter have a table widget?

Tkinter doesn't have a built-in table widget. The closest you can use is a Listbox or a Treeview of the tkinter's sub package ttk.

However, you can use tktable, which is a wrapper around the Tcl/Tk TkTable widget, written by Guilherme Polo. Note: to use this wrapper library you need first to have installed the original Tk's TkTable library, otherwise you will get an "import error".

Table of widgets in Python with Tkinter

You can use the grid geometry manager in a frame to layout the widgets however you want. Here's a simple example:

import Tkinter as tk
import time

class Example(tk.LabelFrame):
def __init__(self, *args, **kwargs):
tk.LabelFrame.__init__(self, *args, **kwargs)
data = [
# Nr. Name Active
[1, "ST", True],
[2, "SO", False],
[3, "SX", True],
]

self.grid_columnconfigure(1, weight=1)
tk.Label(self, text="Nr.", anchor="w").grid(row=0, column=0, sticky="ew")
tk.Label(self, text="Name", anchor="w").grid(row=0, column=1, sticky="ew")
tk.Label(self, text="Active", anchor="w").grid(row=0, column=2, sticky="ew")
tk.Label(self, text="Action", anchor="w").grid(row=0, column=3, sticky="ew")

row = 1
for (nr, name, active) in data:
nr_label = tk.Label(self, text=str(nr), anchor="w")
name_label = tk.Label(self, text=name, anchor="w")
action_button = tk.Button(self, text="Delete", command=lambda nr=nr: self.delete(nr))
active_cb = tk.Checkbutton(self, onvalue=True, offvalue=False)
if active:
active_cb.select()
else:
active_cb.deselect()

nr_label.grid(row=row, column=0, sticky="ew")
name_label.grid(row=row, column=1, sticky="ew")
active_cb.grid(row=row, column=2, sticky="ew")
action_button.grid(row=row, column=3, sticky="ew")

row += 1

def delete(self, nr):
print "deleting...nr=", nr

if __name__ == "__main__":
root = tk.Tk()
Example(root, text="Hello").pack(side="top", fill="both", expand=True, padx=10, pady=10)
root.mainloop()

Creating a table look-a-like Tkinter

What problem are you having? The simple solution is to lay out widgets using grid. You can put whatever type of widget you want in each cell. And yes, labels can have borders. Though, a simple way to do grid lines is to use a padding around each cell, so that the color of the frame will show through the gaps.

Do this in a frame. If you need to be able to scroll the table, put the frame inside a canvas and add scrollbars. There are examples all over the web for how to create a scrollable frame using a canvas.

Here's a really quick example that uses just labels, and doesn't scroll. I'll leave the exact implementation of what you need as an exercise for the reader.

import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
t = SimpleTable(self, 10,2)
t.pack(side="top", fill="x")
t.set(0,0,"Hello, world")

class SimpleTable(tk.Frame):
def __init__(self, parent, rows=10, columns=2):
# use black background so it "peeks through" to
# form grid lines
tk.Frame.__init__(self, parent, background="black")
self._widgets = []
for row in range(rows):
current_row = []
for column in range(columns):
label = tk.Label(self, text="%s/%s" % (row, column),
borderwidth=0, width=10)
label.grid(row=row, column=column, sticky="nsew", padx=1, pady=1)
current_row.append(label)
self._widgets.append(current_row)

for column in range(columns):
self.grid_columnconfigure(column, weight=1)

def set(self, row, column, value):
widget = self._widgets[row][column]
widget.configure(text=value)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()

Which widget do you use for a Excel like table in tkinter?

Tktable is at least arguably the best option, if you need full table support. Briefly, the following example shows how to use it assuming you have it installed. The example is for python3, but for python2 you only need to change the import statement.

import tkinter as tk
import tktable

root = tk.Tk()
table = tktable.Table(root, rows=10, cols=4)
table.pack(side="top", fill="both", expand=True)
root.mainloop()

Tktable can be difficult to install since there is no pip-installable package.

If all you really need is a grid of widgets for displaying and editing data, you can easily build a grid of entry or label widgets. For an example, see this answer to the question Python. GUI(input and output matrices)?



Related Topics



Leave a reply



Submit