问题
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?
回答1:
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.
回答2:
If you browse Tkinter.py, you'll find that rowconfigure is just a reference to grid_rowconfigure.
来源:https://stackoverflow.com/questions/18280824/what-is-correct-widget-rowconfigure-or-widget-grid-rowconfigure