PyGobject(六十二)Gtk.Widget之Gtk.Label

Gtk.Label

Gtk.Label显示文本部件,常用来嵌入其它部件,如 Gtk.Button、Gtk.MenuItem、 Gtk.ComboBox等.

继承关系

这里写图片描述
这里写图片描述

Methods

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

NameTypeFlagsShort Description
anglefloatr/w/enlabel旋转度数
attributesPango.AttrListr/wA list of style attributes to apply to the text of the label
cursor-positionintr光标位置,在第几个字符处
ellipsizePango.EllipsizeModer/w/en如果空间不够显示全部文本如何处理,一般在尾部使用省略号
justifyGtk.Justificationr/w/en多行文本对齐方式
labelstrr/wlabel中的文本
linesintr/w/en文本行数,如果设置了ellipsize
max-width-charsintr/w/en最大宽度
mnemonic-keyvalintr助记符
mnemonic-widgetGtk.Widgetr/w当mnemonic key按下时激活这个部件
patternstrw给指定位置的字符下添加下划线,见例子
selectableboolr/w/en文本是否可以被鼠标选中
selection-boundintrThe position of the opposite end of the selection from the cursor in chars
single-line-modeboolr/w/en是否只显示单行
track-visited-linksboolr/w/enWhether visited links should be tracked
use-markupboolr/w/en是否使用markup语法
use-underlineboolr/w/en下划线是否代表助记符
width-charsintr/w/en宽度
wrapboolr/w/en是否换行
wrap-modePango.WrapModer/w/en换行模式

Signals

NameShort Description
activate-current-linkA keybinding signal which gets emitted when the user activates a link in the label.
activate-linkThe signal which gets emitted to activate a URI.
copy-clipboardThe ::copy-clipboard signal is a keybinding signal which gets emitted to copy the selection to the clipboard.
move-cursorThe ::move-cursor signal is a keybinding signal which gets emitted when the user initiates a cursor movement.
populate-popupThe ::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.




代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值