【发布时间】:2011-08-30 05:41:01
【问题描述】:
在我有限的 OO GUI 编程(主要是 JAVA 和 Tkinter)经验中,我注意到在某些代码中,所有小部件都分配给实例属性,而在其他代码中,即使有也很少。
例如,在 python 文档的 Tkinter 章节的 A Simple Hello World Program 中,两个按钮都分配给 Application 类的实例属性:
class Application(Frame):
...
def createWidgets(self):
self.QUIT = Button(self)
...
self.hi_there = Button(self)
...
def __init__(self, master=None):
Frame.__init__(self, master)
...
self.createWidgets()
另一方面,Tkinter 书籍的Dialog Windows 章节定义了一个对话框支持类,它的任何小部件都没有分配给实例属性:
class Dialog(Toplevel):
def __init__(self, parent, title = None):
Toplevel.__init__(self, parent)
...
body = Frame(self)
...
self.buttonbox()
...
...
def buttonbox(self):
...
box = Frame(self)
w = Button(box, ...)
...
w = Button(box, ...)
...
...
问题陈述
每种方法的优缺点是什么?在某些情况下使用一种方法而不是另一种方法更有意义?
【问题讨论】:
标签: user-interface coding-style