gpt4 book ai didi

python - 带有 Python3 和 Gtk3 的 MVC

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:57 24 4
gpt4 key购买 nike

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

最佳答案

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

关于python - 带有 Python3 和 Gtk3 的 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40498549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com