Gtk.Label
Gtk.Label显示文本部件,常用来嵌入其它部件,如 Gtk.Button、Gtk.MenuItem、 Gtk.ComboBox等.
继承关系
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (str) |
static | new_with_mnemonic (str) |
get_angle () | |
get_attributes () | |
get_current_uri () | |
get_ellipsize () | |
get_justify () | |
get_label () | |
get_layout () | |
get_layout_offsets () | |
get_line_wrap () | |
get_line_wrap_mode () | |
get_lines () | |
get_max_width_chars () | |
get_mnemonic_keyval () | |
get_mnemonic_widget () | |
get_selectable () | |
get_selection_bounds () | |
get_single_line_mode () | |
get_text () | |
get_track_visited_links () | |
get_use_markup () | |
get_use_underline () | |
get_width_chars () | |
get_xalign () | |
get_yalign () | |
select_region (start_offset, end_offset) | |
set_angle (angle) | |
set_attributes (attrs) | |
set_ellipsize (mode) | |
set_justify (jtype) | |
set_label (str) | |
set_line_wrap (wrap) | |
set_line_wrap_mode (wrap_mode) | |
set_lines (lines) | |
set_markup (str) | |
set_markup_with_mnemonic (str) | |
set_max_width_chars (n_chars) | |
set_mnemonic_widget (widget) | |
set_pattern (pattern) | |
set_selectable (setting) | |
set_single_line_mode (single_line_mode) | |
set_text (str) | |
set_text_with_mnemonic (str) | |
set_track_visited_links (track_links) | |
set_use_markup (setting) | |
set_use_underline (setting) | |
set_width_chars (n_chars) | |
set_xalign (xalign) | |
set_yalign (yalign) |
Virtual Methods
do_activate_link (uri) |
do_copy_clipboard () |
do_move_cursor (step, count, extend_selection) |
do_populate_popup (menu) |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
angle | float | r/w/en | label旋转度数 |
attributes | Pango.AttrList | r/w | A list of style attributes to apply to the text of the label |
cursor-position | int | r | 光标位置,在第几个字符处 |
ellipsize | Pango.EllipsizeMode | r/w/en | 如果空间不够显示全部文本如何处理,一般在尾部使用省略号 |
justify | Gtk.Justification | r/w/en | 多行文本对齐方式 |
label | str | r/w | label中的文本 |
lines | int | r/w/en | 文本行数,如果设置了ellipsize |
max-width-chars | int | r/w/en | 最大宽度 |
mnemonic-keyval | int | r | 助记符 |
mnemonic-widget | Gtk.Widget | r/w | 当mnemonic key按下时激活这个部件 |
pattern | str | w | 给指定位置的字符下添加下划线,见例子 |
selectable | bool | r/w/en | 文本是否可以被鼠标选中 |
selection-bound | int | r | The position of the opposite end of the selection from the cursor in chars |
single-line-mode | bool | r/w/en | 是否只显示单行 |
track-visited-links | bool | r/w/en | Whether visited links should be tracked |
use-markup | bool | r/w/en | 是否使用markup语法 |
use-underline | bool | r/w/en | 下划线是否代表助记符 |
width-chars | int | r/w/en | 宽度 |
wrap | bool | r/w/en | 是否换行 |
wrap-mode | Pango.WrapMode | r/w/en | 换行模式 |
Signals
Name | Short Description |
---|---|
activate-current-link | A keybinding signal which gets emitted when the user activates a link in the label. |
activate-link | The signal which gets emitted to activate a URI. |
copy-clipboard | The ::copy-clipboard signal is a keybinding signal which gets emitted to copy the selection to the clipboard. |
move-cursor | The ::move-cursor signal is a keybinding signal which gets emitted when the user initiates a cursor movement. |
populate-popup | The ::populate-popup signal gets emitted before showing the context menu of the label. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 101
TITLE = "Label"
DESCRIPTION = ""
import gi
import webbrowser
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class LabelWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Label Example")
hbox = Gtk.Box(spacing=10)
hbox.set_homogeneous(False)
vbox_left = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
vbox_left.set_homogeneous(False)
vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
vbox_right.set_homogeneous(False)
hbox.pack_start(vbox_left, True, True, 0)
hbox.pack_start(vbox_right, True, True, 0)
label = Gtk.Label("This is a normal label")
# "is" add underline
label.props.pattern = "01234__"
vbox_left.pack_start(label, True, True, 0)
label = Gtk.Label()
label.set_text("This is a left-justified label.\nWith multiple lines.")
label.set_justify(Gtk.Justification.LEFT)
vbox_left.pack_start(label, True, True, 0)
label = Gtk.Label(
"This is a right-justified label.\nWith multiple lines.")
label.set_justify(Gtk.Justification.RIGHT)
vbox_left.pack_start(label, True, True, 0)
label = Gtk.Label("This is an example of a line-wrapped label. It "
"should not be taking up the entire "
"width allocated to it, but automatically "
"wraps the words to fit.\n"
" It supports multiple paragraphs correctly, "
"and correctly adds "
"many extra spaces. ")
label.set_line_wrap(True)
vbox_right.pack_start(label, True, True, 0)
label = Gtk.Label("This is an example of a line-wrapped, filled label. "
"It should be taking "
"up the entire width allocated to it. "
"Here is a sentence to prove "
"my point. Here is another sentence. "
"Here comes the sun, do de do de do.\n"
" This is a new paragraph.\n"
" This is another newer, longer, better "
"paragraph. It is coming to an end, "
"unfortunately.")
label.set_line_wrap(True)
label.set_justify(Gtk.Justification.FILL)
vbox_right.pack_start(label, True, True, 0)
label = Gtk.Label()
label.set_markup("Text can be <small>small</small>, <big>big</big>, "
"<b>bold</b>, <i>italic</i> and even point to "
"<span foreground=\"#FF0000\">GtkLabel红色字体</span>"
"somewhere in the <a href=\"http://www.gtk.org\" "
"title=\"Click to find out more\">internets</a> <a href=\"keynav\">keynav</a>.")
label.connect("activate-link", self.link)
label.set_line_wrap(True)
vbox_left.pack_start(label, True, True, 0)
label = Gtk.Label.new_with_mnemonic(
"_Press Alt + P to select button to the right")
vbox_left.pack_start(label, True, True, 0)
label.set_selectable(True)
button = Gtk.Button(label="Click at your own risk")
label.set_mnemonic_widget(button)
vbox_right.pack_start(button, True, True, 0)
self.add(hbox)
def link(self, widget, url):
if url == 'keynav':
parent = widget.get_toplevel()
markup = """The term <i>keynav</i> is a shorthand for
keyboard navigation and refers to the process of using
a program (exclusively) via keyboard input."""
dialog = Gtk.MessageDialog(transient_for=parent,
destroy_with_parent=True,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text=markup,
use_markup=True)
dialog.run()
dialog.destroy()
else:
webbrowser.open(url)
return True
def main():
window = LabelWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
if __name__ == "__main__":
main()
附录
Pango.EllipsizeMode
class Pango.EllipsizeMode
Bases: GObject.GEnum
NONE = 0
不省略
START = 1
省略开始位置的字符
MIDDLE = 2
省略中间位置的字符
END = 3
省略结尾位置的字符
Pango.WrapMode
class Pango.WrapMode
Bases: GObject.GEnum
WORD = 0
以单词为边界换行
CHAR = 1
以字符为边界换行
WORD_CHAR = 2
wrap lines at word boundaries, but fall back to character boundaries if there is not enough space for a full word.
Gtk.Justification
class Gtk.Justification
Bases: GObject.GEnum
LEFT = 0
The text is placed at the left edge of the label.
RIGHT = 1
The text is placed at the right edge of the label.
CENTER = 2
The text is placed in the center of the label.
FILL = 3
The text is placed is distributed across the label.