问题描述
限时送ChatGPT账号..在基于 这个很棒的 Bryan Oakley 的回答扩展代码时(顺便说一句,他的代码效果很好,只有我无法使填充工作)我发现传递给 .grid() 的参数 ipadx 在线上被忽略了:
While extending the code based on this awesome Bryan Oakley's answer (btw his code works great, it's just me the one that can't make the padding work) I've found that the argument ipadx passed to .grid() is being ignored on the line:
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 没有内部 x 填充的小部件
So I end up getting no internal padding on the entry widgets, they align the text to the right but leave no space before the ttk.Entry border as can be seen on this image: ttk.Entry widgets with no internal x padding
让我惊讶的一件事是 ipady 工作正常.
One thing that amazes me is that ipady works ok.
我尝试过的事情:
tk.Entry 而不是 ttk.Entryipadx 的不同值(完全没有区别)阅读文档以了解是否有其他参数可能会干扰 ipadx,但一无所获(但我认为这是最可能的原因)如果有帮助,我在 Windows 7 上使用 Tkinter 8.6 版和 Python 3.6.2
If it's of any help, I'm using Tkinter version 8.6 and Python 3.6.2 on windows 7
推荐答案
Entry
小部件不考虑ipadx
,但tk.Label
是,所以它依赖于小部件.
The Entry
widget does not take into account the ipadx
, but tk.Label
does, so it is widget dependent.
但是,还有另一种方法可以通过使用样式在 ttk.Entry
中添加填充,使用 style.configure('padded.TEntry', padding=[
:
However, there is another way to add padding inside a ttk.Entry
by using a style, with 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()
这篇关于Tkinter 的 grid() 几何管理器忽略 ipadx 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论