Python设计模式系列之三: 创建型Factory Method模式
2007-03-30 12:55:15 来源:WEB开发网核心提示: 所有这些辅助类都定义好之后,接下去就可以编写PIM类了,Python设计模式系列之三: 创建型Factory Method模式(8),它提供了一个对各种个人信息进行统一管理的框架,其完整的代码如清单8所示
所有这些辅助类都定义好之后,接下去就可以编写PIM类了,它提供了一个对各种个人信息进行统一管理的框架,其完整的代码如清单8所示。
代码清单8:pim.py
from editablephone import EditablePhone
from editableaddressfactory import EditableAddressFactory
from editablephonefactory import EditablePhoneFactory
import Tkinter
class PIM:
""" 个人信息管理 """
# 构造函数
def __init__(self):
mainFrame = Tkinter.Frame()
mainFrame.master.title("PIM")
# 命令按钮
addressButton = Tkinter.Button(mainFrame, width=10, text="Address")
phoneButton = Tkinter.Button(mainFrame, width=10, text="Phone")
commitButton = Tkinter.Button(mainFrame, width=10, text="Commit")
resetButton = Tkinter.Button(mainFrame, width=10, text="Reset")
addressButton.config(command=self.addressClicked)
phoneButton.config(command=self.phoneClicked)
commitButton.config(command=self.commitClicked)
resetButton.config(command=self.resetClicked)
addressButton.grid(row=0, column=1, padx=10, pady=5, stick=Tkinter.E)
phoneButton.grid(row=1, column=1, padx=10, pady=5, stick=Tkinter.E)
commitButton.grid(row=2, column=1, padx=10, pady=5, stick=Tkinter.E)
resetButton.grid(row=3, column=1, padx=10, pady=5, stick=Tkinter.E)
# 用来容纳各类Editor的容器
self.editorFrame = Tkinter.Frame(mainFrame)
self.editorFrame.grid(row=0, column=0, rowspan=4)
self.editorFrame.grid_configure(stick=Tkinter.N, pady=15)
self.editor = Tkinter.Frame(self.editorFrame)
self.editor.grid()
# 个人信息显示区域
self.content = Tkinter.StringVar()
self.contentLabel = Tkinter.Label(mainFrame, width=50, height=5)
self.contentLabel.configure(textvariable=self.content)
self.contentLabel.configure(anchor=Tkinter.W, font="Arial 10 italic bold")
self.contentLabel.configure(relief=Tkinter.RIDGE, pady=5, padx=10)
self.contentLabel.grid(row=4, column=0, columnspan=2)
mainFrame.pack()
mainFrame.mainloop()
# Address按钮的回调函数
def addressClicked(self):
address = EditableAddressFactory().createEditable(self.editorFrame)
self.editor.grid_remove()
self.editor = address.getEditor()
self.editor.getUI().grid()
# Phone按钮的回调函数
def phoneClicked(self):
phone = EditablePhoneFactory().createEditable(self.editorFrame)
self.editor.grid_remove()
self.editor = phone.getEditor()
self.editor.getUI().grid()
# Commit按钮的回调函数
def commitClicked(self):
content = self.editor.getContent()
self.content.set(content)
# Reset按钮的回调函数
def resetClicked(self):
self.editor.resetUI()
# 主函数
if (__name__ == "__main__"):
app = PIM()
更多精彩
赞助商链接