Tkinter几何管理器方法返回None而非控件的设计缘由及修改可行性探讨
注:我意识到这个问题可能带有主观观点,如果有更合适的讨论场所,请告知我,我会移除该问题。感谢您的坦诚!
Tkinter新手极易犯如下错误:
my_button = tkinter.Button(text='Hello').pack()
随后会遇到错误,因为将控件链式调用几何管理器方法(如pack、place或grid)后,my_button的值为None。
因此常规做法是将控件声明与几何管理器调用分开,例如:
my_button = tkinter.Button(text='Hello') my_button.pack()
查看几何管理器类的源码可知,修改为返回调用方法的控件极其简单,只需在每个几何管理器的_configure方法中添加一行return self(我已在下方实现)。
pack
class Pack: """Geometry manager Pack. Base class to use the methods pack_* in every widget.""" def pack_configure(self, cnf={}, **kw): """Pack a widget in the parent widget. Use as options: after=widget - pack it after you have packed widget anchor=NSEW (or subset) - position widget according to given direction before=widget - pack it before you will pack widget expand=bool - expand widget if parent size grows fill=NONE or X or Y or BOTH - fill widget if widget grows in=master - use master to contain this widget in_=master - see 'in' option description ipadx=amount - add internal padding in x direction ipady=amount - add internal padding in y direction padx=amount - add padding in x direction pady=amount - add padding in y direction side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget. """ self.tk.call( ('pack', 'configure', self._w) + self._options(cnf, kw)) return self # return the widget passed to this method pack = configure = config = pack_configure
place
class Place: """Geometry manager Place. Base class to use the methods place_* in every widget.""" def place_configure(self, cnf={}, **kw): """Place a widget in the parent widget. Use as options: in=master - master relative to which the widget is placed in_=master - see 'in' option description x=amount - locate anchor of this widget at position x of master y=amount - locate anchor of this widget at position y of master relx=amount - locate anchor of this widget between 0.0 and 1.0 relative to width of master (1.0 is right edge) rely=amount - locate anchor of this widget between 0.0 and 1.0 relative to height of master (1.0 is bottom edge) anchor=NSEW (or subset) - position anchor according to given direction width=amount - width of this widget in pixel height=amount - height of this widget in pixel relwidth=amount - width of this widget between 0.0 and 1.0 relative to width of master (1.0 is the same width as the master) relheight=amount - height of this widget between 0.0 and 1.0 relative to height of master (1.0 is the same height as the master) bordermode="inside" or "outside" - whether to take border width of master widget into account """ self.tk.call( ('place', 'configure', self._w) + self._options(cnf, kw)) return self # return the widget passed to this method place = configure = config = place_configure
grid
class Grid: """Geometry manager Grid. Base class to use the methods grid_* in every widget.""" # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu) def grid_configure(self, cnf={}, **kw): """Position a widget in the parent widget in a grid. Use as options: column=number - use cell identified with given column (starting with 0) columnspan=number - this widget will span several columns in=master - use master to contain this widget in_=master - see 'in' option description ipadx=amount - add internal padding in x direction ipady=amount - add internal padding in y direction padx=amount - add padding in x direction pady=amount - add padding in y direction row=number - use cell identified with given row (starting with 0) rowspan=number - this widget will span several rows sticky=NSEW - if cell is larger on which sides will this widget stick to the cell boundary """ self.tk.call( ('grid', 'configure', self._w) + self._options(cnf, kw)) return self # return the widget passed to this method grid = configure = config = grid_configure
我的核心问题是:能否有人解释Tkinter此特性的设计依据?这种修改是否值得提交PR/PEP?会对Tkinter造成兼容性破坏吗?
这并非严格意义上的问题,而是在看到大量相关提问后深入研究的结果。
内容的提问来源于stack exchange,提问作者JRiggles
相关产品推荐
相关产品推荐

