Tkinter's grid() geometry manager ignoring ipadx argument

不羁的心 提交于 2021-01-27 20:27:40

问题


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)

from the following script:

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()

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

One thing that amazes me is that ipady works ok.

Things I've tried:

  • tk.Entry instead of ttk.Entry
  • different values for ipadx (makes no difference at all)
  • read the docs to find if another argument could be interfering with ipadx, found nothing (however I regard this as the most probable cause)

If it's of any help, I'm using Tkinter version 8.6 and Python 3.6.2 on windows 7


回答1:


The Entry widget does not take into account the ipadx, but tk.Label does, so it is widget dependent.

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()


来源:https://stackoverflow.com/questions/46460265/tkinters-grid-geometry-manager-ignoring-ipadx-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!