Gtk.InfoBar
Gtk.InfoBar用于显示信息给用户而不显示一个对话框Dialog。它往往位于文档的顶部或底部。其API与Dialog的非常像
继承关系
Gtk.InfoBar是Gtk.Box的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new () |
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
Name | Type | Flags | Short Description |
---|---|---|---|
message-type | Gtk.MessageType | r/w/c/en | The type of message |
show-close-button | bool | r/w/c/en | Whether to include a standard close button |
Signals
Name | Short Description |
---|---|
close | The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to dismiss the info bar. |
response | Emitted 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