python非web应用程序模式
在web设计中,我使用MVC模式,但有时我需要创建非web应用程序。 这可能是一些解析器或GUI实用程序。python非web应用程序模式,python,parsing,user-interface,Python,Parsing,User Interface,在web设计中,我使用MVC模式,但有时我需要创建非web应用程序。 这可能是一些解析器或GUI实用程序。 这种应用程序的经典解决方案是什么?MVC同样适用于非web应用程序。唯一改变的是视图(GUI控件而不是web控件)和控制器可以/必须处理的输入类型。MVC同样适用于非web应用程序。唯一需要改变的是视图(GUI控件而不是web控件)和控制器可以/必须处理的输入类型。实用程序类型程序最直接的方法是类似于以下伪代码的Python,带有PyGTK提示。设想一个以某种方式操纵文件的实用程序 cla
这种应用程序的经典解决方案是什么?MVC同样适用于非web应用程序。唯一改变的是视图(GUI控件而不是web控件)和控制器可以/必须处理的输入类型。MVC同样适用于非web应用程序。唯一需要改变的是视图(GUI控件而不是web控件)和控制器可以/必须处理的输入类型。实用程序类型程序最直接的方法是类似于以下伪代码的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:文件
是您的模型,窗口
是视图,应用程序
是控制器
当然,还有其他更正式的方法。大多数Python GUI工具包的Wiki都有关于可能的体系结构的文章。例如,请参见wxPython。PyGTK还有一个MVC框架,名为
我的观点是,除非您完全确定您需要这样一种正式的方法,否则最好使用上面的代码。Web框架受益于更正式的方法,因为有更多的部分需要连接:HTTP请求、HTML、JavaScript、SQL、业务逻辑、表示逻辑、路由等等,即使对于最简单的应用程序也是如此。使用典型的Python GUI应用程序,您只需使用Python处理业务逻辑和事件处理。对于实用程序类型的程序,最简单的方法是类似于以下伪代码的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:文件
是您的模型,窗口
是视图,应用程序
是控制器
当然,还有其他更正式的方法。大多数Python GUI工具包的Wiki都有关于可能的体系结构的文章。例如,请参见wxPython。PyGTK还有一个MVC框架,名为
我的观点是,除非您完全确定您需要这样一种正式的方法,否则最好使用上面的代码。Web框架受益于更正式的方法,因为有更多的部分需要连接:HTTP请求、HTML、JavaScript、SQL、业务逻辑、表示逻辑、路由等等,即使对于最简单的应用程序也是如此。使用典型的pythongui应用程序,您只需处理业务逻辑和事件处理,所有这些都是用Python编写的