简体   繁体   English

带有Python3和Gtk3的MVC

[英]MVC with Python3 and Gtk3

Is there any way to use MVC pattern with Python3 and Gtk3? 有什么方法可以在Python3和Gtk3中使用MVC模式吗? I found a library called pygtkmvc , but it's based on pygtk , that is, gtk2 . 我找到了一个名为pygtkmvc的库,但它基于pygtk ,即gtk2

MVC is a pattern, you don't need a library in order to use it. MVC是一种模式,您不需要使用库即可使用它。 It would go something like this contrived example: 它会像下面这样的人为的例子:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject

class Model(object):
    @property
    def greetee(self):
        return 'World'

class Controller(object):
    def __init__(self, model, view):
        self._model = model
        self._view = view

        self._view.connect('button-clicked', self._on_button_clicked)
        self._view.connect('destroy', Gtk.main_quit)

        self._view.show_all()

    def _on_button_clicked(self, button, *args):
        greetee = self._model.greetee
        self._view.set_text('Hello, {}'.format(greetee))
        self._view.change_page(self._view.GREETING_PAGE)

class View(Gtk.Window):
    BUTTON_PAGE = 'button'
    GREETING_PAGE = 'greeting'

    __gsignals__ = {
        'button-clicked': (GObject.SIGNAL_RUN_FIRST, None, ())
    }

    def __init__(self, **kw):
        super(View, self).__init__(default_width=400, default_height=300, **kw)

        self._stack = Gtk.Stack(transition_duration=500,
            transition_type=Gtk.StackTransitionType.CROSSFADE)
        self._button = Gtk.Button(label='Click me', halign=Gtk.Align.CENTER,
            valign=Gtk.Align.CENTER)
        self._label = Gtk.Label()
        self._stack.add_named(self._button, self.BUTTON_PAGE)
        self._stack.add_named(self._label, self.GREETING_PAGE)
        self.add(self._stack)

        self._button.connect('clicked', self._on_clicked)

    def _on_clicked(self, button, *args):
        self.emit('button-clicked')

    def change_page(self, page):
        self._stack.props.visible_child_name = page

    def set_text(self, text):
        self._label.props.label = text

Controller(Model(), View())
Gtk.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM