在网页设计中我使用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. 非android,非ios,非web应用程序
- 2. JQuery Web应用程序设计模式
- 3. Python web应用程序
- 4. Python到Web应用程序
- 5. 应用程序服务器用于非Web应用程序
- 6. 在Web应用程序中编辑模式查看模式
- 7. 非Web应用程序中的OpenSessionInViewInterceptor
- 8. 常规(非Web应用程序)JVM
- 9. 在非Web应用程序中的Foursquare
- 10. NHibernate与非Web应用程序的StructureMap
- 11. AWS部署非web应用程序
- 12. 适用于多模块Web应用程序的设计模式
- 13. Python应用程序模板
- 14. 使用python开发web应用程序
- 15. 使用Python的web应用程序web.py
- 16. 使用Python刮取Web应用程序
- 17. Python web应用程序部署多个应用程序实例
- 18. Django的web应用程序和桌面Python应用程序
- 19. Convet现有的Web应用程序UI进入响应式Web应用程序
- 20. 适用于非Web应用程序的Docker + IDE的Python工作流程
- 21. 使用Java web应用程序模板
- 22. 做Rails5 API模式的应用程序和非API模式的应用程序共享代码彼此?
- 23. 使用Python Flask构建渐进式Web应用程序
- 24. Android模式应用程序?
- 25. 发布部署python web应用程序
- 26. Python应用程序的Web前端
- 27. 分析一个Python Web应用程序
- 28. 与Web应用程序在Python处理
- 29. 如何调整Python Web应用程序?
- 30. python上的实时web应用程序