MVC与Python3和Gtk3

时间:2016-11-09 00:15:02

标签: python python-3.x gtk gtk3

有没有办法在Python3和Gtk3中使用MVC模式? 我找到了一个名为pygtkmvc的库,但它基于pygtk,即gtk2

1 个答案:

答案 0 :(得分:2)

MVC是一种模式,你不需要一个库来使用它。它会像这个人为的例子:

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