根据this awesome Bryan Oakley的回答扩展代码(顺便说一下,他的代码很好用,只有我能做出无法填充的代码)I&# 39;我发现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工作正常。
我尝试的事情:
如果有任何帮助,我在Windows 7上使用Tkinter版本8.6和Python 3.6.2
答案 0 :(得分:2)
Entry
窗口小部件不会考虑ipadx
,但会考虑tk.Label
,因此它取决于窗口小部件。
但是,还有另一种方法可以使用样式ttk.Entry
在style.configure('padded.TEntry', padding=[<left>, <top>, <right>, <bottom>])
内添加填充:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style(root)
style.configure('padded.TEntry', padding=[5, 3, 5, 3])
e = ttk.Entry(root, justify='right', style='padded.TEntry')
e.grid()
root.mainloop()