Gtk.ShortcutsWindow
Gtk.ShortcutsWindow一显示有关应用程序的键盘快捷键和手势的简要信息。该快捷键可以分组,你可以有多个节在此窗口中,对应于应用程序的主要模式。
此外,该快捷方式可以通过当前视图进行过滤,以避免显示与当前上下文无关的信息。
继承关系
Gtk.ShortcutsWindow是Gtk.Window的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|
Virtual Methods
do_close () |
do_search () |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
section-name | str | r/w | Section Name |
view-name | str | r/w | View Name |
Signals
Name | Short Description |
---|---|
close | The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to close the window. |
search | The ::search signal is a keybinding signal which gets emitted when the user uses a keybinding to start a search. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/18
# section 041
#
# author: xiaosanyu
# website: yuxiaosan.tk \
# http://blog.youkuaiyun.com/a87b01c14
# created: 16/7/18
TITLE = "ShortcutsWindow"
DESCRIPTION = """
A Gtk.ShortcutsWindow shows brief information about the keyboard shortcuts and gestures of an application.
The shortcuts can be grouped, and you can have multiple sections in this window,
corresponding to the major modes of your application
"""
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Gio
import os
class ShortcutsWindow():
def __init__(self):
builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), "../../Data/shortcuts.glade"))
builder.connect_signals(self)
window = builder.get_object("window1")
window.connect("destroy", Gtk.main_quit)
window.show_all()
Gtk.main()
@staticmethod
def show_shortcuts(window, name, view=None):
path = "../../Data/%s.glade" % name
builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), path))
overlay = builder.get_object(name)
overlay.set_transient_for(window)
overlay.props.view_name = view
overlay.show()
def builder_shortcuts(self, widget):
self.show_shortcuts(widget, "shortcuts-builder")
def gedit_shortcuts(self, widget):
self.show_shortcuts(widget, "shortcuts-gedit")
def clocks_shortcuts(self, widget):
self.show_shortcuts(widget, "shortcuts-clocks")
def clocks_shortcuts_stopwatch(self, widget):
self.show_shortcuts(widget, "shortcuts-clocks", "stopwatch")
def boxes_shortcuts(self, widget):
self.show_shortcuts(widget, "shortcuts-boxes")
def boxes_shortcuts_wizard(self, widget):
self.show_shortcuts(widget, "shortcuts-boxes", "wizard")
def boxes_shortcuts_display(self, widget):
self.show_shortcuts(widget, "shortcuts-boxes", "display")
def main():
ShortcutsWindow()
if __name__ == "__main__":
main()
shortcuts.glade
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.17 -->
<object class="GtkWindow" id="window1">
<property name="title" translatable="yes">Shortcuts</property>
<child>
<object class="GtkBox">
<property name="visible">1</property>
<property name="orientation">vertical</property>
<property name="margin">50</property>
<property name="spacing">10</property>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Builder</property>
<signal name="clicked" handler="builder_shortcuts" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">GEdit</property>
<signal name="clicked" handler="gedit_shortcuts" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Clocks - All</property>
<signal name="clicked" handler="clocks_shortcuts" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Clocks - Stopwatch</property>
<signal name="clicked" handler="clocks_shortcuts_stopwatch" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Boxes</property>
<signal name="clicked" handler="boxes_shortcuts" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Boxes - Wizard</property>
<signal name="clicked" handler="boxes_shortcuts_wizard" swapped="1" object="window1"/>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label">Boxes - Display</property>
<signal name="clicked" handler="boxes_shortcuts_display" swapped="1" object="window1"/>
</object>
</child>
</object>
</child>
</object>
</interface>
shortcuts-boxes.glade
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.17 -->
<object class="GtkShortcutsWindow" id="shortcuts-boxes">
<property name="modal">1</property>
<child>
<object class="GtkShortcutsSection">
<property name="visible">1</property>
<property name="section-name">shortcuts</property>
<property name="max-height">12</property>
<!-- Overview shortcuts -->
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Overview</property>
<property name="view">overview</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">F1</property>
<property name="title" translatable="yes">Help</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><Ctrl>n</property>
<property name="title" translatable="yes">Create a new box</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><Ctrl>f</property>
<property name="title" translatable="yes">Search</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><Ctrl>k</property>
<property name="title" translatable="yes">Keyboard shortcuts</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><Ctrl>q</property>
<property name="title" translatable="yes">Close Window/Quit Boxes</property>
</object>
</child>
</object>
</child>
<!-- Wizard and Properties shortcuts -->
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Box Creation and Properties</property>
<property name="view">wizard</property>
<!-- LTR -->
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">ltr</property>
<property name="accelerator"><Alt>Right</property>
<property name="title" translatable="yes">Switch to the next page</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">ltr</property>
<property name="accelerator"><Alt>Left</property>
<property name="title" translatable="yes">Switch to the previous page</property>
</object>
</child>
<!-- RTL -->
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">rtl</property>
<property name="accelerator"><Alt>Left</property>
<property name="title" translatable="yes">Switch to the next page</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">rtl</property>
<property name="accelerator"><Alt>Right</property>
<property name="title" translatable="yes">Switch to the previous page</property>
</object>
</child>
</object>
</child>
<!-- Display shortcuts -->
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Box Display</property>
<property name="view">display</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Control_L+Alt_L</property>
<property name="title" translatable="yes">Grab/Ungrab keyboard</property>
</object>
</child>
<!-- LTR -->
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">ltr</property>
<property name="accelerator"><Alt>Left</property>
<property name="title" translatable="yes">Back to overview</property>
</object>
</child>
<!-- RTL -->
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="direction">rtl</property>
<property name="accelerator"><Alt>Right</property>
<property name="title" translatable="yes">Back to overview</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><Ctrl>q</property>
<property name="title" translatable="yes">Close window/Quit Boxes</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">F11</property>
<property name="title" translatable="yes">Fullscreen/Restore from fullscreen</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
shortcuts-clocks.glade
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.17 -->
<object class="GtkShortcutsWindow" id="shortcuts-clocks">
<property name="modal">1</property>
<child>
<object class="GtkShortcutsSection">
<property name="visible">1</property>
<property name="section-name">shortcuts</property>
<property name="max-height">10</property>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">General</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>Page_Down</property>
<property name="title" translatable="yes">Go to the next section</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>Page_Up</property>
<property name="title" translatable="yes">Go to the previous section</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><alt>Q</property>
<property name="title" translatable="yes">Quit</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><alt>Right</property>
<property name="direction">ltr</property>
<property name="title" translatable="yes">Forward</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>Left</property>
<property name="direction">ltr</property>
<property name="title" translatable="yes">Back</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><alt>Left</property>
<property name="direction">rtl</property>
<property name="title" translatable="yes">Forward</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>Right</property>
<property name="direction">rtl</property>
<property name="title" translatable="yes">Back</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="view">world</property>
<property name="title" translatable="yes">World Clocks</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>N</property>
<property name="title" translatable="yes">Add a world clock</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>S</property>
<property name="title" translatable="yes">Select world clocks</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="view">alarm</property>
<property name="title" translatable="yes">Alarm</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>N</property>
<property name="title" translatable="yes">Add an alarm</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>S</property>
<property name="title" translatable="yes">Select alarms</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="view">stopwatch</property>
<property name="title" translatable="yes">Stopwatch</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Return space</property>
<property name="title" translatable="yes">Start / Stop / Continue</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">L</property>
<property name="title" translatable="yes">Lap</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Delete</property>
<property name="title" translatable="yes">Reset</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="view">timer</property>
<property name="title" translatable="yes">Timer</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Return space</property>
<property name="title" translatable="yes">Start / Stop / Pause</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Delete</property>
<property name="title" translatable="yes">Reset</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
shortcuts-gedit.glade
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.17 -->
<object class="GtkShortcutsWindow" id="shortcuts-gedit">
<property name="modal">1</property>
<child>
<object class="GtkShortcutsSection">
<property name="visible">1</property>
<property name="section-name">shortcuts</property>
<property name="max-height">12</property>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Touchpad gestures</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="shortcut-type">gesture-two-finger-swipe-right</property>
<property name="title" translatable="yes">Switch to the next document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="shortcut-type">gesture-two-finger-swipe-left</property>
<property name="title" translatable="yes">Switch to the previous document</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Documents</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>N</property>
<property name="title" translatable="yes">Create new document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>O</property>
<property name="title" translatable="yes">Open a document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>S</property>
<property name="title" translatable="yes">Save the document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>W</property>
<property name="title" translatable="yes">Close the document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl><Alt>Page_Down</property>
<property name="title" translatable="yes">Switch to the next document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl><Alt>Page_Up</property>
<property name="title" translatable="yes">Switch to the previous document</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Find and Replace</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>F</property>
<property name="title" translatable="yes">Find</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>G</property>
<property name="title" translatable="yes">Find the next match</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl><Shift>G</property>
<property name="title" translatable="yes">Find the previous match</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>H</property>
<property name="title" translatable="yes">Find and Replace</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl><Shift>K</property>
<property name="title" translatable="yes">Clear highlight</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>I</property>
<property name="title" translatable="yes">Go to line</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Tools</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><shift>F7</property>
<property name="title" translatable="yes">Check spelling</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
<property name="title" translatable="yes">Miscellaneous</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">F11</property>
<property name="title" translatable="yes">Fullscreen on / off</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator"><ctrl>P</property>
<property name="title" translatable="yes">Print the document</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">Insert</property>
<property name="title" translatable="yes">Toggle insert / overwrite</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
代码解析:
使用Gtk.Builder加载主界面文件shortcuts.glade.
有一个注意的事项就是下面这句话
<signal name="clicked" handler="builder_shortcuts" swapped="1" object="window1"/>
意思相当于
button.connect_object("clicked",self.builder_shortcuts,window1)
回调函数的第一个参数不是button,而是window1
所以在show_shortcuts()方法中使用
overlay.set_transient_for(window)
来设置overlay的父窗口
另一个注意的事情就是view_name属性的使用
overlay.props.view_name = view
表示用来显示view_name为view的视图,其他的不予显示。
代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728