PyGobject(八十六)Gtk.Application
2016-08-02 19:46
489 查看
GtkApplication
Actions
Menus
Command Line
Methods
Virtual Methods
Properties
Signals
例子
一
二
虽然Gtk.Application能够和普通Gtk.Windows一起正常的工作,不过通常建议与Gtk.ApplicationWindow一起使用。
使用Actions的原因是为了将UI界面和业务逻辑进行分离。通常我们使用Gio.Action的子类Gio.SimpleAction来定义一个Action
很多类,如Gio.MenuItem和Gtk.ModelButton支持在属性中设置Action名称。
这些Action可以一起分组到Gio.ActionGroup并使用Gtk.Widget.insert_action_group(name, group))将其添加到一个Widget中。这个name就是前缀。当添加到Gtk.ApplicationWindow中时这个前缀就是“win”。当你引用Action时,需要使用完整的Action名称,如“app.about”。
你也可以很容易通过设置Gio.Menu文件中的“accel”属性或使用Gtk.Application.add_accelerator方法做出Action和按键绑定。
您可以使用HANDLES_COMMAND_LINE允许在Gio.Application.do_command_line方法中获取给定的命令并自定它要执行的事务。使用Gio.Application.add_main_option()方法将命令添加到命令行中。
使用HANDLES_OPEN会在Gio.Application.do_open()方法打开你参数列表中所给的文件
如果你的应用程序已经打开这些都将被发送到现有实例,除非你用NON_UNIQUE允许多个实例。
代码:
代码:
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
Actions
Menus
Command Line
Methods
Virtual Methods
Properties
Signals
例子
一
二
Gtk.Application
Gtk.Application是在gtk+3.0才引入的一个新类,它包含许多现代的应用需求,如处理多个实例,D-Bus的激活,打开文件,在命令行解析命令,开机/关机,菜单管理,窗口管理等等虽然Gtk.Application能够和普通Gtk.Windows一起正常的工作,不过通常建议与Gtk.ApplicationWindow一起使用。
Actions
Gio.Action表示应用程序或部件的一个任务,通常有一个名字来标识这个任务。可以在运行时启用或者禁用Gio.Action,它们可以被激活或这改变状态(如果它们包含状态)。使用Actions的原因是为了将UI界面和业务逻辑进行分离。通常我们使用Gio.Action的子类Gio.SimpleAction来定义一个Action
很多类,如Gio.MenuItem和Gtk.ModelButton支持在属性中设置Action名称。
这些Action可以一起分组到Gio.ActionGroup并使用Gtk.Widget.insert_action_group(name, group))将其添加到一个Widget中。这个name就是前缀。当添加到Gtk.ApplicationWindow中时这个前缀就是“win”。当你引用Action时,需要使用完整的Action名称,如“app.about”。
你也可以很容易通过设置Gio.Menu文件中的“accel”属性或使用Gtk.Application.add_accelerator方法做出Action和按键绑定。
Menus
通常我们使用XML文件来定义菜单,并引用前面提到的Action。 Gtk.Application使用Gtk.Application.set_app_menu()或Gtk.Application.set_menubar()方法来设置菜单。Command Line
创建Gtk.Application需要Gio.ApplicationFlags参数。使用这个你可以让它处理它自身的所有行为,还可以处理更多的自定义行为。您可以使用HANDLES_COMMAND_LINE允许在Gio.Application.do_command_line方法中获取给定的命令并自定它要执行的事务。使用Gio.Application.add_main_option()方法将命令添加到命令行中。
使用HANDLES_OPEN会在Gio.Application.do_open()方法打开你参数列表中所给的文件
如果你的应用程序已经打开这些都将被发送到现有实例,除非你用NON_UNIQUE允许多个实例。
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (application_id, flags) |
add_accelerator (accelerator, action_name, parameter) | |
add_window (window) | |
get_accels_for_action (detailed_action_name) | |
get_actions_for_accel (accel) | |
get_active_window () | |
get_app_menu () | |
get_menu_by_id (id) | |
get_menubar () | |
get_window_by_id (id) | |
get_windows () | |
inhibit (window, flags, reason) | |
is_inhibited (flags) | |
list_action_descriptions () | |
prefers_app_menu () | |
remove_accelerator (action_name, parameter) | |
remove_window (window) | |
set_accels_for_action (detailed_action_name, accels) | |
set_app_menu (app_menu) | |
set_menubar (menubar) | |
uninhibit (cookie) |
Virtual Methods
do_window_added (window) |
do_window_removed (window) |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
active-window | Gtk.Window | r | The window which most recently had focus |
app-menu | Gio.MenuModel | r/w | The Gio.MenuModel for the application menu |
menubar | Gio.MenuModel | r/w | The Gio.MenuModel for the menubar |
register-session | bool | r/w | Register with the session manager |
Signals
Name | Short Description |
---|---|
window-added | Emitted when a Gtk.Window is added to application through Gtk.Application.add_window(). |
window-removed | Emitted when a Gtk.Window is removed from application, either as a side-effect of being destroyed or explicitly through Gtk.Application.remove_window(). |
例子
一
代码:
#!/usr/bin/env python3 # Created by xiaosanyu at 16/6/16 # section 134 TITLE = "Application" DESCRIPTION = """ Gtk.Application is a class that handles many important aspects of a GTK+ application in a convenient fashion, without enforcing a one-size-fits-all application model While Gtk.Application works fine with plain Gtk.Windows, it is recommended to use it together with Gtk.ApplicationWindow. """ import sys import gi gi.require_version('Gtk', '3.0') from gi.repository import GLib, Gio, Gtk, GdkPixbuf import os # This would typically be its own file MENU_XML = """ <?xml version="1.0" encoding="UTF-8"?> <interface> <menu id="app-menu"> <section> <attribute name="label" translatable="yes">Change label</attribute> <item> <attribute name="action">win.change_label</attribute> <attribute name="target">String 1</attribute> <attribute name="label" translatable="yes">String 1</attribute> </item> <item> <attribute name="action">win.change_label</attribute> <attribute name="target">String 2</attribute> <attribute name="label" translatable="yes">String 2</attribute> </item> <item> <attribute name="action">win.change_label</attribute> <attribute name="target">String 3</attribute> <attribute name="label" translatable="yes">String 3</attribute> </item> </section> <section> <item> <attribute name="action">win.maximize</attribute> <attribute name="label" translatable="yes">Maximize</attribute> </item> </section> <section> <item> <attribute name="action">app.about</attribute> <attribute name="label" translatable="yes">_About</attribute> </item> <item> <attribute name="action">app.quit</attribute> <attribute name="label" translatable="yes">_Quit</attribute> <attribute name="accel"><Primary>q</attribute> </item> </section> </menu> </interface> """ class AppWindow(Gtk.ApplicationWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # set logo icon = GdkPixbuf.Pixbuf.new_from_file(os.path.join(os.path.dirname(__file__), "../Data/gtk-logo-rgb.gif")) # transparent icon = icon.add_alpha(True, 0xff, 0xff, 0xff) self.set_default_icon(icon) # This will be in the windows group and have the "win" prefix max_action = Gio.SimpleAction.new_stateful("maximize", None, GLib.Variant.new_boolean(False)) max_action.connect("change-state", self.on_maximize_toggle) self.add_action(max_action) # Keep it in sync with the actual state self.connect("notify::is-maximized", lambda obj, pspec: max_action.set_state( GLib.Variant.new_boolean(obj.props.is_maximized))) lbl_variant = GLib.Variant.new_string("String 1") lbl_action = Gio.SimpleAction.new_stateful("change_label", lbl_variant.get_type(), lbl_variant) lbl_action.connect("change-state", self.on_change_label_state) self.add_action(lbl_action) self.label = Gtk.Label(label=lbl_variant.get_string(), margin=30) self.add(self.label) self.label.show() def on_change_label_state(self, action, value): action.set_state(value) self.label.set_text(value.get_string()) def on_maximize_toggle(self, action, value): action.set_state(value) if value.get_boolean(): self.maximize() else: self.unmaximize() class Application(Gtk.Application): def __init__(self, *args, **kwargs): super().__init__(*args, application_id="org.example.myapp", flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, **kwargs) self.window = None # 设置命令行参数 # $python3 /Applications/Project/Python/project/PYGUI/pygtk3/demos/Application/Application.py --help self.add_main_option("test", ord("t"), GLib.OptionFlags.NONE, GLib.OptionArg.NONE, "Command line test", None) def do_startup(self): Gtk.Application.do_startup(self) action = Gio.SimpleAction.new("about", None) action.connect("activate", self.on_about) self.add_action(action) action = Gio.SimpleAction.new("quit", None) action.connect("activate", self.on_quit) self.add_action(action) builder = Gtk.Builder.new_from_string(MENU_XML, -1) self.set_app_menu(builder.get_object("app-menu")) 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 = AppWindow(application=self, title="Main Window") self.window.present() # $python3 /Applications/Project/Python/project/PYGUI/pygtk3/demos/Application/Application.py --test # Test argument recieved def do_command_line(self, command_line): options = command_line.get_options_dict() if options.contains("test"): # This is printed on the main instance print("Test argument recieved") self.activate() return 0 def on_about(self, action, param): about_dialog = Gtk.AboutDialog(transient_for=self.window, modal=True) about_dialog.run() about_dialog.destroy() def on_quit(self, action, param): if self.window: self.window.destroy() self.quit() def main(): app = Application() app.run(sys.argv) if __name__ == "__main__": main()
二
代码:
#!/usr/bin/env python3 # section 135 # -*- Mode: Python; py-indent-offset: 4 -*- # vim: tabstop=4 shiftwidth=4 expandtab # # Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 # USA TITLE = "Application main window" DESCRIPTION = """ Demonstrates a typical application window with menubar, toolbar, statusbar. """ import webbrowser import os import gi gi.require_version('Gtk', '3.0') gi.require_version('GdkPixbuf', '2.0') from gi.repository import GdkPixbuf, Gtk infobar = None window = None messagelabel = None def widget_destroy(widget, button): widget.destroy() def activate_action(action, user_data=None): global window name = action.get_name() _type = type(action) if name == 'DarkTheme': value = action.get_active() settings = Gtk.Settings.get_default() settings.set_property('gtk-application-prefer-dark-theme', value) return dialog = Gtk.MessageDialog(message_type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.CLOSE, text='You activated action: "%s" of type %s' % (name, _type)) # FIXME: this should be done in the constructor dialog.set_transient_for(window) dialog.connect('response', widget_destroy) dialog.show() def activate_radio_action(action, current, user_data=None): global infobar global messagelabel name = current.get_name() _type = type(current) active = current.get_active() value = current.get_current_value() if active: text = 'You activated radio action: "%s" of type %s.\n Current value: %d' % (name, _type, value) messagelabel.set_text(text) infobar.set_message_type(Gtk.MessageType(value)) infobar.show() def update_statusbar(buffer, statusbar): statusbar.pop(0) count = buffer.get_char_count() iter = buffer.get_iter_at_mark(buffer.get_insert()) row = iter.get_line() col = iter.get_line_offset() msg = 'Cursor at row %d column %d - %d chars in document' % (row, col, count) statusbar.push(0, msg) def mark_set_callback(buffer, new_location, mark, data): update_statusbar(buffer, data) def about_cb(widget, user_data=None): global window authors = ['John (J5) Palmieri', 'Tomeu Vizoso', 'and many more...'] documentors = ['David Malcolm', 'Zack Goldberg', 'and many more...'] license = """ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the Gnome Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ dirname = os.path.abspath(os.path.dirname(__file__)) filename = os.path.join(dirname, '../Data', 'gtk-logo-rgb.gif') pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename) transparent = pixbuf.add_alpha(True, 0xff, 0xff, 0xff) about = Gtk.AboutDialog(parent=window, program_name='GTK+ Code Demos', version='0.1', copyright='(C) 2010 The PyGI Team', license=license, website='http://live.gnome.org/PyGI', comments='Program to demonstrate PyGI functions.', authors=authors, documenters=documentors, logo=transparent, title='About GTK+ Code Demos') about.connect("activate-link", click_link) about.connect('response', widget_destroy) about.show() def click_link(widget, url): webbrowser.open(url) return True action_entries = ( ("FileMenu", None, "_File"), # name, stock id, label ("OpenMenu", None, "_Open"), # name, stock id, label ("PreferencesMenu", None, "_Preferences"), # name, stock id, label ("ColorMenu", None, "_Color"), # name, stock id, label ("ShapeMenu", None, "_Shape"), # name, stock id, label ("HelpMenu", None, "_Help"), # name, stock id, label ("New", Gtk.STOCK_NEW, # name, stock id "_New", "<control>N", # label, accelerator "Create a new file", # tooltip activate_action), ("File1", None, # name, stock id "File1", None, # label, accelerator "Open first file", # tooltip activate_action), ("Save", Gtk.STOCK_SAVE, # name, stock id "_Save", "<control>S", # label, accelerator "Save current file", # tooltip activate_action), ("SaveAs", Gtk.STOCK_SAVE, # name, stock id "Save _As...", None, # label, accelerator "Save to a file", # tooltip activate_action), ("Quit", Gtk.STOCK_QUIT, # name, stock id "_Quit", "<control>Q", # label, accelerator "Quit", # tooltip activate_action), ("About", None, # name, stock id "_About", "<control>A", # label, accelerator "About", # tooltip about_cb), ("Logo", "demo-gtk-logo", # name, stock id None, None, # label, accelerator "GTK+", # tooltip activate_action), ) toggle_action_entries = ( ("Bold", Gtk.STOCK_BOLD, # name, stock id "_Bold", "<control>B", # label, accelerator "Bold", # tooltip activate_action, True), # is_active ("DarkTheme", None, # name, stock id "_Prefer Dark Theme", None, # label, accelerator "Prefer Dark Theme", # tooltip activate_action, False), # is_active ) (COLOR_RED, COLOR_GREEN, COLOR_BLUE) = range(3) color_action_entries = ( ("Red", None, # name, stock id "_Red", "<control>R", # label, accelerator "Blood", COLOR_RED), # tooltip, value ("Green", None, # name, stock id "_Green", "<control>G", # label, accelerator "Grass", COLOR_GREEN), # tooltip, value ("Blue", None, # name, stock id "_Blue", "<control>B", # label, accelerator "Sky", COLOR_BLUE), # tooltip, value ) (SHAPE_SQUARE, SHAPE_RECTANGLE, SHAPE_OVAL) = range(3) shape_action_entries = ( ("Square", None, # name, stock id "_Square", "<control>S", # label, accelerator "Square", SHAPE_SQUARE), # tooltip, value ("Rectangle", None, # name, stock id "_Rectangle", "<control>R", # label, accelerator "Rectangle", SHAPE_RECTANGLE), # tooltip, value ("Oval", None, # name, stock id "_Oval", "<control>O", # label, accelerator "Egg", SHAPE_OVAL), # tooltip, value ) ui_info = """ <ui> <menubar name='MenuBar'> <menu action='FileMenu'> <menuitem action='New'/> <menuitem action='Open'/> <menuitem action='Save'/> <menuitem action='SaveAs'/> <separator/> <menuitem action='Quit'/> </menu> <menu action='PreferencesMenu'> <menuitem action='DarkTheme'/> <menu action='ColorMenu'> <menuitem action='Red'/> <menuitem action='Green'/> <menuitem action='Blue'/> </menu> <menu action='ShapeMenu'> <menuitem action='Square'/> <menuitem action='Rectangle'/> <menuitem action='Oval'/> </menu> <menuitem action='Bold'/> </menu> <menu action='HelpMenu'> <menuitem action='About'/> </menu> </menubar> <toolbar name='ToolBar'> <toolitem action='Open'> <menu action='OpenMenu'> <menuitem action='File1'/> </menu> </toolitem> <toolitem action='Quit'/> <separator action='Sep1'/> <toolitem action='Logo'/> </toolbar> </ui> """ def _quit(*args): Gtk.main_quit() def register_stock_icons(): """ This function registers our custom toolbar icons, so they can be themed. It's totally optional to do this, you could just manually insert icons and have them not be themeable, especially if you never expect people to theme your app. """ ''' item = Gtk.StockItem() item.stock_id = 'demo-gtk-logo' item.label = '_GTK!' item.modifier = 0 item.keyval = 0 item.translation_domain = None Gtk.stock_add(item, 1) ''' factory = Gtk.IconFactory() factory.add_default() filename = os.path.join(os.path.dirname(__file__), '../Data', 'gtk-logo-rgb.gif') pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename) transparent = pixbuf.add_alpha(True, 0xff, 0xff, 0xff) icon_set = Gtk.IconSet.new_from_pixbuf(transparent) factory.add('demo-gtk-logo', icon_set) class ToolMenuAction(Gtk.Action): __gtype_name__ = "GtkToolMenuAction" def do_create_tool_item(self): return Gtk.MenuToolButton() def main(): global infobar global window global messagelabel register_stock_icons() window = Gtk.Window() window.set_title('Application Window') window.set_icon_name('gtk-open') window.connect_after('destroy', _quit) table = Gtk.Table(n_rows=1, n_columns=5, homogeneous=False) window.add(table) action_group = Gtk.ActionGroup(name='AppWindowActions') open_action = ToolMenuAction(name='Open', stock_id=Gtk.STOCK_OPEN, label='_Open', tooltip='Open a file') action_group.add_action(open_action) action_group.add_actions(action_entries) action_group.add_toggle_actions(toggle_action_entries) action_group.add_radio_actions(color_action_entries, COLOR_RED, activate_radio_action) action_group.add_radio_actions(shape_action_entries, SHAPE_SQUARE, activate_radio_action) merge = Gtk.UIManager() merge.insert_action_group(action_group, 0) window.add_accel_group(merge.get_accel_group()) merge.add_ui_from_string(ui_info) bar = merge.get_widget('/MenuBar') bar.show() table.attach(bar, 0, 1, 0, 1, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0) bar = merge.get_widget('/ToolBar') bar.show() table.attach(bar, 0, 1, 1, 2, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0) infobar = Gtk.InfoBar() infobar.set_no_show_all(True) messagelabel = Gtk.Label() messagelabel.show() infobar.get_content_area().pack_start(messagelabel, True, True, 0) infobar.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK) infobar.connect('response', lambda a, b: Gtk.Widget.hide(a)) table.attach(infobar, 0, 1, 2, 3, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0) sw = Gtk.ScrolledWindow(hadjustment=None, vadjustment=None) sw.set_shadow_type(Gtk.ShadowType.IN) table.attach(sw, 0, 1, 3, 4, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0) contents = Gtk.TextView() contents.grab_focus() sw.add(contents) # Create statusbar statusbar = Gtk.Statusbar() table.attach(statusbar, 0, 1, 4, 5, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0) # show text widget info in the statusbar buffer = contents.get_buffer() buffer.connect('changed', update_statusbar, statusbar) buffer.connect('mark_set', mark_set_callback, statusbar) update_statusbar(buffer, statusbar) window.set_default_size(200, 200) window.show_all() Gtk.main() if __name__ == '__main__': main()
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
相关文章推荐
- PyGobject(九)布局容器之Button篇——Gtk.ToggleButton
- PyGobject(七十)Gtk.Widget之Gtk.ProgressBar
- PyGobject(十)布局容器之Button篇——Gtk.CheckButton
- PyGobject(七十一)Gtk.Widget之Gtk.Entry
- PyGobject(十一)布局容器之Button篇——Gtk.RadioButton
- PyGobject(七十二)Gtk.Widget之Gtk.SearchEntry
- PyGobject(八十)Gtk.Clipboard
- PyGobject(十二)布局容器之Button篇——Gtk.MenuButton
- PyGobject(七十三)Gtk.Widget之Gtk.SpinButton
- 如何在Python开发环境Wing IDE中使用GTK和PyGObject
- PyGobject(七十四)Gtk.Widget之Gtk.Scale
- PyGobject(八十三)Gtk.SizeGroup
- Python开发环境Wing IDE如何使用GTK和PyGObject
- PyGobject(十四)布局容器之Button篇——Gtk.LockButton
- PyGobject(六十一)Gtk.Widget
- PyGobject(七十五)Gtk.Widget之Gtk.Scrollbar
- Python开发环境Wing IDE使用手册之GTK和PyGObject
- PyGobject(十五)布局容器之Button篇——Gtk.ScaleButton
- PyGobject(七十六)Gtk.Widget之Gtk.Spinner
- GTK+ / Gnome Application Development