Gtk.Toolbar
Gtk.Toolbar工具栏
继承关系
Gtk.Toolbar是Gtk.Container的直接子类
Methods
| 方法修饰词 | 方法名及参数 |
|---|---|
| static | new () |
| get_drop_index (x, y) | |
| get_icon_size () | |
| get_item_index (item) | |
| get_n_items () | |
| get_nth_item (n) | |
| get_relief_style () | |
| get_show_arrow () | |
| get_style () | |
| insert (item, pos) | |
| set_drop_highlight_item (tool_item, index_) | |
| set_icon_size (icon_size) | |
| set_show_arrow (show_arrow) | |
| set_style (style) | |
| unset_icon_size () | |
| unset_style () |
Virtual Methods
| do_orientation_changed (orientation) |
| do_popup_context_menu (x, y, button_number) |
| do_style_changed (style) |
Properties
| Name | Type | Flags | Short Description |
|---|---|---|---|
| icon-size | Gtk.IconSize | r/w/en | Size of icons in this toolbar |
| icon-size-set | bool | r/w | Whether the icon-size property has been set |
| show-arrow | bool | r/w/en | If an arrow should be shown if the toolbar doesn’t fit |
| toolbar-style | Gtk.ToolbarStyle | r/w/en | 显示样式 |
Signals
| Name | Short Description |
|---|---|
| focus-home-or-end | A keybinding signal used internally by GTK+. |
| orientation-changed | Emitted when the orientation of the toolbar changes. |
| popup-context-menu | Emitted when the user right-clicks the toolbar or uses the keybinding to display a popup menu. |
| style-changed | Emitted when the style of the toolbar changes. |
例子
一
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/3
# section 072
TITLE = "Toolbar"
DESCRIPTION = """
Menus group commands that we can use in application.
Toolbars provide a quick access to the most frequently used commands.
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio
class ToolbarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Toolbar Example")
self.set_size_request(250, 200)
self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("gray"))
toolbar = Gtk.Toolbar()
# 显示图片和文本
toolbar.set_style(Gtk.ToolbarStyle.BOTH)
newtb = Gtk.ToolButton(Gtk.STOCK_NEW)
opentb = Gtk.ToolButton(Gtk.STOCK_OPEN)
savetb = Gtk.ToolButton(Gtk.STOCK_SAVE)
sep = Gtk.SeparatorToolItem()
quittb = Gtk.ToolButton(Gtk.STOCK_QUIT)
toolbar.insert(newtb, 0)
toolbar.insert(opentb, 1)
toolbar.insert(savetb, 2)
toolbar.insert(sep, 3)
toolbar.insert(quittb, 4)
quittb.connect("clicked", self.on_destroy)
vbox = Gtk.VBox(False, 2)
vbox.pack_start(toolbar, False, False, 0)
self.add(vbox)
def on_destroy(self, widget):
self.destroy()
Gtk.main_quit()
def main():
win = ToolbarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
代码解析:
toolbar = Gtk.Toolbar()
# 显示图片和文本
toolbar.set_style(Gtk.ToolbarStyle.BOTH)
创建一个Gtk.Toolbar,设置样式为显示文本和图片
newtb = Gtk.ToolButton(Gtk.STOCK_NEW)
opentb = Gtk.ToolButton(Gtk.STOCK_OPEN)
savetb = Gtk.ToolButton(Gtk.STOCK_SAVE)
sep = Gtk.SeparatorToolItem()
quittb = Gtk.ToolButton(Gtk.STOCK_QUIT)
创建四个Gtk.ToolButton和一个分隔线Gtk.SeparatorToolItem
toolbar.insert(newtb, 0)
toolbar.insert(opentb, 1)
toolbar.insert(savetb, 2)
toolbar.insert(sep, 3)
toolbar.insert(quittb, 4)
插入到toolbar中
二弹出菜单
代码
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/3
# section 073
TITLE = "Popmenu"
DESCRIPTION = """
show Popmenu on the Toolbar
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio, GLib
items = ["EditCopy", "EditPaste", "EditSomething"]
class ToolbarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Toolbar Popmenu")
self.set_size_request(250, 200)
toolbar = Gtk.Toolbar()
# 只显示图片
toolbar.set_style(Gtk.ToolbarStyle.ICONS)
newtb = Gtk.ToolButton(Gtk.STOCK_NEW)
opentb = Gtk.ToolButton(Gtk.STOCK_OPEN)
savetb = Gtk.ToolButton(Gtk.STOCK_SAVE)
sep = Gtk.SeparatorToolItem()
quittb = Gtk.ToolButton(Gtk.STOCK_QUIT)
toolbar.insert(newtb, 0)
toolbar.insert(opentb, 1)
toolbar.insert(savetb, 2)
toolbar.insert(sep, 3)
toolbar.insert(quittb, 4)
quittb.connect("clicked", self.on_destroy)
vbox = Gtk.VBox(False, 2)
vbox.pack_start(toolbar, False, False, 0)
menu = Gtk.Menu.new()
for item in items:
item = Gtk.MenuItem(label=item)
item.connect("activate", self.on_menu_selected)
item.show()
menu.append(item)
toolbar.connect("popup_context_menu", self.popmenu, menu)
self.add(vbox)
@staticmethod
def popmenu(toolbar, x, y, button, menu):
menu.popup(None, None, None, None, button, Gtk.get_current_event_time())
return True
@staticmethod
def on_menu_selected(widget, *args):
print("Menu item " + widget.get_label() + " was selected")
def on_destroy(self, widget):
self.destroy()
Gtk.main_quit()
def main():
win = ToolbarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
代码解析:
基本同例一,就是多了个弹出菜单
三
代码
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/3
# section 074
TITLE = "Redo & Undo"
DESCRIPTION = """
The following example demonstrates, how we can inactivate toolbar buttons on the toolbar.
It is a common practise in GUI programming. For example the save button.
If we save all changes of our document to the disk, the save button is inactivated in most text editors.
This way the application indicates to the user, that all changes are already saved.
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio
class ToolbarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Toolbar Redo & Undo")
self.set_size_request(250, 200)
self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("gray"))
self.count = 2
toolbar = Gtk.Toolbar()
# 只显示图片
toolbar.set_style(Gtk.ToolbarStyle.ICONS)
self.undo = Gtk.ToolButton(Gtk.STOCK_UNDO)
self.redo = Gtk.ToolButton(Gtk.STOCK_REDO)
sep = Gtk.SeparatorToolItem()
quittb = Gtk.ToolButton(Gtk.STOCK_QUIT)
toolbar.insert(self.undo, 0)
toolbar.insert(self.redo, 1)
toolbar.insert(sep, 2)
toolbar.insert(quittb, 3)
self.undo.connect("clicked", self.on_undo)
self.redo.connect("clicked", self.on_redo)
quittb.connect("clicked", self.on_destroy)
vbox = Gtk.VBox(False, 2)
vbox.pack_start(toolbar, False, False, 0)
self.add(vbox)
def on_undo(self, widget):
self.count -= 1
if self.count <= 0:
self.undo.set_sensitive(False)
self.redo.set_sensitive(True)
def on_redo(self, widget):
self.count += 1
if self.count >= 5:
self.redo.set_sensitive(False)
self.undo.set_sensitive(True)
def on_destroy(self, widget):
self.destroy()
Gtk.main_quit()
def main():
win = ToolbarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
这个例子也比较简单,不讲解了
附录
Gtk.ToolbarStyle
class Gtk.ToolbarStyle
Bases: GObject.GEnum
ICONS = 0
只显示图标
TEXT = 1
只显示文本.
BOTH = 2
显示文本和图标
BOTH_HORIZ = 3
文本和图标水平显示而不是垂直.貌似没效果
代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728
Gtk.Toolbar教程

本文通过三个实例介绍了Gtk.Toolbar的使用方法,包括如何创建工具栏、设置样式、添加按钮、实现弹出菜单以及根据状态激活或禁用按钮。
307

被折叠的 条评论
为什么被折叠?



