PyGobject(五十五)布局容器之Toolbar

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gtk.Toolbar

Gtk.Toolbar工具栏

继承关系

Gtk.Toolbar是Gtk.Container的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
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

NameTypeFlagsShort Description
icon-sizeGtk.IconSizer/w/enSize of icons in this toolbar
icon-size-setboolr/wWhether the icon-size property has been set
show-arrowboolr/w/enIf an arrow should be shown if the toolbar doesn’t fit
toolbar-styleGtk.ToolbarStyler/w/en显示样式

Signals

NameShort Description
focus-home-or-endA keybinding signal used internally by GTK+.
orientation-changedEmitted when the orientation of the toolbar changes.
popup-context-menuEmitted when the user right-clicks the toolbar or uses the keybinding to display a popup menu.
style-changedEmitted 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值