最近tkinterにテーブルを作成しようとしました。私は幸いにも、私のためにうまく動作するコードを見つけることができましたが、このテーブルにヘッダーを追加する必要があります。あなたは、私はフォーラムでここに見つけるコードにそれを組み込む方法の提案を持っていますか?:tkinterのpythonで作成された既存のテーブルにヘッダを追加する
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")
述べたように私はテーブルの上に動的に表示されるヘッダーを持っていたいと思います。どんな助けもありがとうございます。ありがとう
最後に努力しなくても、例を求めてはいけません。何か試しましたか?もしそうなら、何を試しましたか?あなたは[良い質問をする方法](https://stackoverflow.com/help/how-to-ask)と[最小限で完全であり、検証可能な例](https://stackoverflow.com/)の恩恵を受けるかもしれません。ヘルプ/ mcve) –