When using grid geometry manager. Let's say you have:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
ttk.Button(root, text="Hello World").grid(sticky=tk.NSEW)
root.mainloop()
The part where you specify the weight of the row/column can also be coded up as:
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
For this example, what is correct method: widget.rowconfigure or widget.grid_rowconfigure? and why?
Bonus: From an implementation POV, why do both work?
grid_rowconfigure(). It takes params such as weight and row/col value. The widget. rowconfigure() is sometimes used in place of widget.
ipadx, ipady − How many pixels to pad widget, horizontally and vertically, inside widget's borders. padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders. row − The row to put widget in; default the first row that is still empty.
If you want the widget to expand to fill up the entire cell, grid it with a sticky value of nsew (north, south, east, west), meaning it will stick to every side. This is shown in the bottom left widget in the above figure. Most widgets have options that can control how they are displayed if they are larger than needed.
Important: pack(), place(), and grid() should not be combined in the same master window. Instead choose one and stick with it.
widget.rowconfigure
is literally just an alias for widget.grid_rowconfigure
. In the source code for tkinter is this line of code:
rowconfigure = grid_rowconfigure
I don't know for a fact, but I suspect that widget.rowconfigure
was just added for convenience. Frankly, I didn't even know it existed until I read this question.
In my opinion, grid_rowconfigure
is the proper name to use. I say that because tkinter is an interface to an underlying tcl/tk interpreter, and in tcl/tk the command is grid rowconfigure
. Since most tkinter functions mirror the naming conventions of the tcl/tk functions as close as possible, grid_rowconfigure
is the natural choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With