Gtk.ApplicationWindow

Gtk.ApplicationWindow是Gtk.Window子类,它提供了一些额外的功能使其能够与Gtk.Application更好的集成。

这个类实现了Gio.ActionGroup和Gio.ActionMap接口,能够方便的添加Actions。在Gtk.ApplicationWindow中使用add_action()添加的action,默认前缀为“win”,在Gtk.Application中使用add_action()添加的action默认前缀为“app”

继承关系

Gtk.ApplicationWindow是Gtk.Window的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (application)
get_help_overlay ()
get_id ()
get_show_menubar ()
set_help_overlay (help_overlay)
set_show_menubar (show_menubar)

Virtual Methods

Properties

NameTypeFlagsShort Description
show-menubarboolr/w/c/en是否显示menubar

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/17
# section 039
# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.csdn.net/a87b01c14
# created: 16/7/17

TITLE = "ApplicationWindow"
DESCRIPTION = """
Gtk.ApplicationWindow is a Gtk.Window subclass that offers some extra functionality
for better integration with Gtk.Application features. Notably,
it can handle both the application menu as well as the menubar
"""

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib, Gio

MENU_XML = """
<interface>
    <menu id='menubar'>
        <submenu>
            <attribute name="label">_Edit</attribute>
            <item>
                 <attribute name="label" translatable="yes">_Copy</attribute>
                 <attribute name="action">win.copy</attribute>
            </item>
            <item>
                 <attribute name="label" translatable="yes">_Paste</attribute>
                 <attribute name="action">win.paste</attribute>
            </item>
        </submenu>
    </menu>
</interface>
"""


class Application(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, application_id="org.gtk.Example.GtkApplicationWindow",
                         flags=Gio.ApplicationFlags.FLAGS_NONE, **kwargs)
        self.window = None

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

        builder = Gtk.Builder.new_from_string(MENU_XML, -1)

        menubar = builder.get_object("menubar")
        self.set_menubar(menubar)

    def do_activate(self):
        # We only allow a single window and raise any existing ones
        if not self.window:
            # Windows are associated with the application
            # when the last one is closed the application shuts down
            self.window = Gtk.ApplicationWindow(application=self, title="ApplicationWindow")
            self.window.set_size_request(200, 200)
            label = Gtk.Label()
            self.window.add(label)
            action = Gio.SimpleAction.new("copy", None)
            action.connect("activate", self.on_copy, label)
            self.window.add_action(action)

            action = Gio.SimpleAction.new("paste", None)
            action.connect("activate", self.on_paste, label)
            self.window.add_action(action)

        self.window.show_all()

    @staticmethod
    def on_copy(action, param, label):
        label.set_label(action.get_name())

    @staticmethod
    def on_paste(action, param, label):
        label.set_label(action.get_name())


def main():
    app = Application()
    app.run()


if __name__ == "__main__":
    main()

代码解析:
MENU_XML字符串定义了一个menu

自定义类Application继承自Gtk.Application,并初始化

class Application(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, application_id="org.gtk.Example.GtkApplicationWindow",
                         flags=Gio.ApplicationFlags.FLAGS_NONE, **kwargs)
        self.window = None

在do_startup虚方法中,使用Gtk.Builder加载设置菜单

builder = Gtk.Builder.new_from_string(MENU_XML, -1)
menubar = builder.get_object("menubar")
self.set_menubar(menubar)

在do_activate虚方法中,创建一个Gtk.ApplicationWindow

self.window = Gtk.ApplicationWindow(application=self, title="ApplicationWindow")

设置其大小,并向其中添加一个Gtk.Label

self.window.set_size_request(200, 200)
label = Gtk.Label()
self.window.add(label)

创建两个Action,并添加到Gtk.ApplicationWindow中

action = Gio.SimpleAction.new("copy", None)
action.connect("activate", self.on_copy, label)
self.window.add_action(action)

action = Gio.SimpleAction.new("paste", None)
action.connect("activate", self.on_paste, label)
self.window.add_action(action)

显示Gtk.ApplicationWindow

self.window.show_all()





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐