【发布时间】:2017-09-28 02:59:41
【问题描述】:
在根据this awesome Bryan Oakley 的回答扩展代码时(顺便说一句,他的代码效果很好,只是我无法使填充工作)我发现参数 ipadx 传递给 .grid()被忽略就行了:
e.grid(row=row, column=column, sticky=tk.N+tk.E+tk.S+tk.W, ipadx=5, ipady=3)
来自以下脚本:
import tkinter as tk
from tkinter import ttk
class SimpleTableInput(tk.Frame):
def __init__(self, parent, rows, columns):
tk.Frame.__init__(self, parent)
self._entry = {}
self.rows = rows
self.columns = columns
# create the table of widgets
for row in range(self.rows):
for column in range(self.columns):
index = (row, column)
e = ttk.Entry(self, justify='right')
e.grid(row=row, column=column, sticky=tk.N+tk.E+tk.S+tk.W, ipadx=5, ipady=3)
#e.grid(row=row, column=column, sticky="nsew", ipadx=5, ipady=3)
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())
current_row.append(self._entry[index].get())
result.append(current_row)
return result
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.table = SimpleTableInput(self, 3, 4)
self.submit = ttk.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()
所以我最终在条目小部件上没有内部填充,它们将文本向右对齐,但在 ttk.Entry 边框之前没有留出空间,如图所示:ttk.Entry widgets with no internal x padding
让我吃惊的一点是 ipady 运行良好。
我尝试过的事情:
- tk.Entry 代替 ttk.Entry
- ipadx 的不同值(完全没有区别)
- 阅读文档以查找是否有其他参数可能干扰 ipadx,但一无所获(但我认为这是最可能的原因)
如果有任何帮助,我在 Windows 7 上使用 Tkinter 8.6 版和 Python 3.6.2
【问题讨论】:
标签: python python-3.x tkinter ttk