在网页设计中我使用MVC模式,但有时我需要创建非web应用程序。 这可能是一些解析器或GUI实用程序。 这种应用程序的典型解决方案是什么模式?python非web应用程序模式
1
A
回答
3
MVC与非Web应用程序一样适用。唯一改变的是View(GUI控件而不是Web控件)以及Controller可以/必须处理的输入类型。
1
实用程序类型程序最直接的方法就像下面的伪代码 - 是Python与PyGTK的提示。想象一下以某种方式操作文件的实用程序。
class File(object):
"""This class handles various filesystem-related tasks."""
def __init__(self, path):
pass
def open(self):
pass
def rename(self, new_name):
pass
def move(self, to):
pass
class Window(gtk.Window):
"""This class is the actual GUI window with widgets."""
def __init__(self):
self.entry_rename = gtk.Entry()
self.entry_move = gtk.Entry()
self.btn_quit = gtk.Button('Quit')
class App(object):
"""This is your main app that displays the GUI and responds to signals."""
def __init__(self):
self.window = Window()
# signal handlers
self.window.connect('destroy', self.on_quit)
self.window.entry_rename.connect('changed', self.on_rename_changed)
self.window.entry_move.connect('changed', self.on_move_changed)
self.window.btn_quit.connect('clicked', self.on_quit)
# and so on...
def on_quit(self):
"""Quit the app."""
pass
def on_rename_changed(self):
"""User typed something into an entry box, do something with text."""
f = File('somefile.txt')
f.rename(self.entry_rename.get_text())
def on_move_changed(self):
"""User typed something into another entry box, do something with text."""
f = File('somefile.txt')
f.move(self.entry_move.get_text())
你可以认为这是一个非正式的MVC:File
是你的模型,Window
是视图和App
是控制器。
当然,还有其他更正式的方法。大多数Python GUI工具包的Wiki都有关于可能的arhitectures的文章。例如,参见wxPython wiki article on MVC。还有一个PyGTK的MVC框架,名为pygtkmvc。
我有一个意见,除非你确定你需要这样一个正式的方法,你最好使用类似以上的代码。 Web框架受益于更正式的方法,因为还有更多要连接的部分:HTTP请求,HTML,JavaScript,SQL,业务逻辑,表示逻辑,路由等,即使是最简单的应用程序也是如此。使用典型的Python GUI应用程序,您只需要使用Python处理业务逻辑和事件处理。
相关问题
- 1. JQuery Web应用程序设计模式
- 2. Python到Web应用程序
- 3. Python web应用程序
- 4. AWS部署非web应用程序
- 5. 在非Web应用程序中的Foursquare
- 6. NHibernate与非Web应用程序的StructureMap
- 7. 适用于多模块Web应用程序的设计模式
- 8. Python应用程序模板
- 9. 使用python开发web应用程序
- 10. 使用Python刮取Web应用程序
- 11. Python web应用程序部署多个应用程序实例
- 12. 做Rails5 API模式的应用程序和非API模式的应用程序共享代码彼此?
- 13. 适用于非Web应用程序的Docker + IDE的Python工作流程
- 14. 使用Java web应用程序模板
- 15. 使用Python Flask构建渐进式Web应用程序
- 16. Python Web服务的Java应用程序?
- 17. Python应用程序的Web前端
- 18. 与Web应用程序在Python处理
- 19. 如何在codeiginiter web应用程序中添加python模块?
- 20. 注册用户时创建Web应用程序和模式
- 21. Web服务应用程序的设计模式(使用APNS)
- 22. 用于Web应用程序的SQL Compact 4文件模式
- 23. iPhone/Android Web应用程序模板
- 24. JSF模块化Web应用程序
- 25. MVC Web应用程序模板
- 26. 模板ASP.NET MVC2 Web应用程序
- 27. Struts 2模块化Web应用程序
- 28. 规模Azure的Web应用程序
- 29. 模糊Struts2 web应用程序
- 30. .NET Web应用程序模板库