What is correct: widget.rowconfigure or widget.grid_rowconfigure?

試著忘記壹切 提交于 2020-01-03 10:42:31

问题


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

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