PyGobject(三十三)布局容器之ApplicationWindow
Gtk.ApplicationWindow是Gtk.Window子类,它提供了一些额外的功能使其能够与Gtk.Application更好的集成。这个类实现了Gio.ActionGroup和Gio.ActionMap接口,能够方便的添加Actions。在Gtk.ApplicationWindow中使用add_action()添加的action默认前缀为"win",Gtk.Application添加的
·
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
方法修饰词 | 方法名及参数 |
---|---|
static | new (application) |
get_help_overlay () | |
get_id () | |
get_show_menubar () | |
set_help_overlay (help_overlay) | |
set_show_menubar (show_menubar) |
Virtual Methods
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
show-menubar | bool | r/w/c/en | 是否显示menubar |
Signals
Name | Short 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()
更多推荐
已为社区贡献7条内容
所有评论(0)