MVC与Python3和Gtk3 [英] MVC with Python3 and Gtk3
本文介绍了MVC与Python3和Gtk3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以在Python3和Gtk3中使用MVC模式?
我找到了一个名为 pygtkmvc
的库,但它基于 pygtk
,也就是 gtk2
。
Is there any way to use MVC pattern with Python3 and Gtk3?
I found a library called pygtkmvc
, but it's based on pygtk
, that is, gtk2
.
推荐答案
MVC是一种模式,您不需要库使用它。它会像这个人为的例子一样:
MVC is a pattern, you don't need a library in order to use it. 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()
这篇关于MVC与Python3和Gtk3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文