开发者

python non-web application pattern

开发者 https://www.devze.com 2023-04-11 02:40 出处:网络
In web design I use MVC pattern, but sometimes I need to create non-web application. This may be some parser or GUI utility program.

In web design I use MVC pattern, but sometimes I need to create non-web application. This may be some parser or GUI utility program. What pattern is a classic solutio开发者_开发技巧n for this kind of application?


MVC is just as applicable to non-web applications. The only things that change are the View (GUI controls instead of web controls) and the types of input the Controller can/must deal with.


The most straightforward approach for utility type program would be something like the following pseudocode-ish Python with hints of PyGTK. Imagine a utility that manipulates files in a certain fashion.

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())

You can think of this as an informal MVC: File is you model, Window is the view and App is the controller.

Of course, there are other, more formal approaches. Wikis of most Python GUI toolkits have articles about possible arhitectures. See, for example, the wxPython wiki article on MVC. There is also an MVC framework for PyGTK, called pygtkmvc.

I have an opinion that unless you are absolutely sure that you need such a formal approach, you're better off using something like the code above. Web frameworks benefit from a more formal approach because there are many more pieces to connect: HTTP requests, HTML, JavaScript, SQL, business logic, presentation logic, routing and so on even for the simplest apps. With your typical Python GUI app, you just have to handle the business logic and event handling, all in Python.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号