通过动作改变属性

Change properties through actions

参见下面的示例。

#!/usr/bin/env python3

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

class ApplicationWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_title("Application")
        self.set_default_size(200, 200)

        grid = Gtk.Grid()
        self.add(grid)

        menubutton = Gtk.MenuButton()
        grid.attach(menubutton, 0, 0, 1, 1)

        label = Gtk.Label.new('AnyLabel')
        grid.attach(label, 0, 1, 1, 1)

        menumodel = Gio.Menu()
        menubutton.set_menu_model(menumodel)
        menumodel.append("New", "app.new")
        menumodel.append("Quit", "app.quit")
        menumodel.append("CLabel", "app.clabel")

class Application(Gtk.Application):

    def do_activate(self):
        window = ApplicationWindow(application=self)
        window.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)

        new_action = Gio.SimpleAction.new("new", None)
        new_action.connect("activate", self.new_callback)
        self.add_action(new_action)

        quit_action = Gio.SimpleAction.new("quit", None)
        quit_action.connect("activate", self.quit_callback)
        self.add_action(quit_action)

        clabel_action = Gio.SimpleAction.new("clabel", None)
        clabel_action.connect("activate", self.clabel_callback)
        self.add_action(clabel_action)

    def new_callback(self, action, parameter):
        print("You clicked New")

    def quit_callback(self, action, parameter):
        print("You clicked Quit")
        self.quit()

    def clabel_callback(self, action, parameter):
        """How to change the label here"""


application = Application()
exit_status = application.run(sys.argv)
sys.exit(exit_status)

如何将 label 中的 clabel_callback 方法更改为 Application class 中的方法?

clabel_callback 方法如何访问 label 并修改任何 属性?

[off]通知无聊!它没有意识到有时候代码值一千字

您可能希望该操作成为您的 ApplicationWindow 的一部分,然后您将引用 "win.clabel"。