PyGobject(四十三)布局容器之InfoBar

Gtk.InfoBar

Gtk.InfoBar用于显示信息给用户而不显示一个对话框Dialog。它往往位于文档的顶部或底部。其API与Dialog的非常像

继承关系

Gtk.InfoBar是Gtk.Box的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
add_action_widget (child, response_id)
add_button (button_text, response_id)
get_action_area ()
get_content_area ()
get_message_type ()
get_show_close_button ()
response (response_id)
set_default_response (response_id)
set_message_type (message_type)
set_response_sensitive (response_id, setting)
set_show_close_button (setting)

Virtual Methods

do_close ()
do_response (response_id)

Properties

NameTypeFlagsShort Description
message-typeGtk.MessageTyper/w/c/enThe type of message
show-close-buttonboolr/w/c/enWhether to include a standard close button

Signals

NameShort Description
closeThe ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to dismiss the info bar.
responseEmitted when an action widget is clicked or the application programmer calls Gtk.Dialog.response().

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# section 060
# -*- 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 = "Info Bars"
DESCRIPTION = """
Info bar widgets are used to report important messages to the user.
"""

import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class InfobarApp:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_title('Info Bars')
        self.window.set_border_width(8)
        self.window.connect('destroy', Gtk.main_quit)

        vbox = Gtk.VBox(spacing=0)
        self.window.add(vbox)

        bar = Gtk.InfoBar()
        bar.connect('response', self.on_bar_response)
        vbox.pack_start(bar, False, False, 5)
        bar.set_message_type(Gtk.MessageType.INFO)
        button = Gtk.Button.new_from_icon_name("help-about", Gtk.IconSize.BUTTON)
        bar.add_action_widget(button, Gtk.ButtonsType.OK)
        label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.INFO')
        bar.get_content_area().pack_start(label, False, False, 5)

        bar = Gtk.InfoBar()
        vbox.pack_start(bar, False, False, 5)
        bar.set_message_type(Gtk.MessageType.WARNING)
        label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.WARNING')
        bar.get_content_area().pack_start(label, False, False, 5)

        bar = Gtk.InfoBar()
        bar.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
        bar.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
        bar.connect('response', self.on_bar_response)
        vbox.pack_start(bar, False, False, 5)
        bar.set_message_type(Gtk.MessageType.QUESTION)
        label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.QUESTION')
        bar.get_content_area().pack_start(label, False, False, 5)

        bar = Gtk.InfoBar()
        vbox.pack_start(bar, False, False, 5)
        bar.set_message_type(Gtk.MessageType.ERROR)
        label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.ERROR')
        bar.get_content_area().pack_start(label, False, False, 5)

        bar = Gtk.InfoBar()
        vbox.pack_start(bar, False, False, 5)
        bar.set_message_type(Gtk.MessageType.OTHER)
        label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.OTHER')
        bar.get_content_area().pack_start(label, False, False, 5)

        frame = Gtk.Frame(label="Info bars")
        vbox.pack_start(frame, False, False, 8)

        vbox2 = Gtk.VBox(spacing=8)
        vbox2.set_border_width(8)
        frame.add(vbox2)

        # Standard message dialog
        label = Gtk.Label(label='An example of different info bars')
        vbox2.pack_start(label, False, False, 0)

        self.window.show_all()

    def on_bar_response(self, info_bar, response_id):
        dialog = Gtk.MessageDialog(transient_for=self.window,
                                   modal=True,
                                   destroy_with_parent=True,
                                   message_type=Gtk.MessageType.INFO,
                                   buttons=Gtk.ButtonsType.OK,
                                   text='You clicked on an info bar')
        dialog.format_secondary_text('Your response has id %d' % response_id)
        dialog.run()
        dialog.destroy()


def main():
    InfobarApp()
    Gtk.main()


if __name__ == '__main__':
    main()

代码解析
几个InfoBar使用比较相同,这里只介绍第一个

bar = Gtk.InfoBar()

创建一个InfoBar

bar.connect('response', self.on_bar_response)

连接到response信号

vbox.pack_start(bar, False, False, 5)

将bar添加到box布局容器中

bar.set_message_type(Gtk.MessageType.INFO)

设置infobar类型

button = Gtk.Button.new_from_icon_name("help-about", Gtk.IconSize.BUTTON)

创建一个图片Button

bar.add_action_widget(button, Gtk.ButtonsType.OK)

将button添加到infobar的action area

label = Gtk.Label(label='This is an info bar with message type Gtk.MessageType.INFO')

创建一个Label

bar.get_content_area().pack_start(label, False, False, 5)

将Label添加到infobar的content area





代码下载地址:http://download.csdn.net/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、付费专栏及课程。

余额充值