将标头添加到在tkinter python中创建的现有表

时间:2017-09-22 20:17:34

标签: python tkinter

我最近尝试在tkinter中创建一个表。我幸运地找到了一些对我来说很好的代码,但是,我需要在此表中添加标题。您有任何建议如何将其纳入我在论坛上找到的代码中吗?:

import tkinter as tk

class SimpleTableInput(tk.Frame):
    def __init__(self, parent, rows, columns):
        tk.Frame.__init__(self, parent)

        self._entry = {}
        self.rows = rows
        self.columns = columns

        # register a command to use for validation
        vcmd = (self.register(self._validate), "%P")

        # create the table of widgets
        for row in range(self.rows):
            for column in range(self.columns):
                index = (row, column)
                e = tk.Entry(self, validate="key", validatecommand=vcmd)
                e.grid(row=row, column=column, stick="nsew")
                self._entry[index] = e
        # adjust column weights so they all expand equally
        for column in range(self.columns):
            self.grid_columnconfigure(column, weight=1)
        # designate a final, empty row to fill up any extra space
        self.grid_rowconfigure(rows, weight=1)

    def get(self):
        '''Return a list of lists, containing the data in the table'''
        result = []
        for row in range(self.rows):
            current_row = []
            for column in range(self.columns):
                index = (row, column)
                current_row.append(self._entry[index].get())
            result.append(current_row)
        return result

    def _validate(self, P):
        '''Perform input validation. 

        Allow only an empty value, or a value that can be converted to a float
        '''
        if P.strip() == "":
            return True

        try:
            f = float(P)
        except ValueError:
            self.bell()
            return False
        return True

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.table = SimpleTableInput(self, 3, 4)
        self.submit = tk.Button(self, text="Submit", command=self.on_submit)
        self.table.pack(side="top", fill="both", expand=True)
        self.submit.pack(side="bottom")

    def on_submit(self):
        print(self.table.get())


root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()

我已尝试过以下内容,但显然不是我所追求的目标:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        names = ["header1", "header2", "header3"]
        self.label = tk.Label(self, text=names)   
        self.table = SimpleTableInput(self, 3, 4)
        self.submit = tk.Button(self, text="Submit", command=self.on_submit)
        self.label.pack(side="top")
        self.table.pack(side="top", fill="both", expand=True)
        self.submit.pack(side="bottom")

如下所述,我希望在表格上方动态显示标题。任何帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:0)

在表格之前添加标签将添加表格标题。

更新此部分代码:

self.label = tk.Label(self, text="Table Header")
self.table = SimpleTableInput(self, 3, 4)
self.submit = tk.Button(self, text="Submit", command=self.on_submit)
self.label.pack(side="top")
self.table.pack(fill="both", expand=True)
self.submit.pack(side="bottom")

我没有再重复给定的代码让你考虑一下。以下是您可能会发现的参考label in TKinter