Gtk.LinkButton
继承关系
Gtk.LinkButton是带有链接文本的按钮。Gtk.LinkButton是Gtk.Button的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (uri) |
static | new_with_label (uri, label) |
get_uri () | |
get_visited () | |
set_uri (uri) | |
set_visited (visited) |
Virtual Methods
do_activate_link () |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
uri | str | r/w | 当前button所绑定的uri |
visited | bool | r/w/en | 这个链接是否被访问过 |
Signals
Name | Short Description |
---|---|
activate-link | ::activate-link 每当链接被点击时,发送此信号. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 009
TITLE = "LinkButton"
DESCRIPTION = """
A Gtk.LinkButton is a Gtk.Button with a hyperlink, similar to the one used by web browsers,
which triggers an action when clicked.
It is useful to show quick links to resources.
The ::activate-link signal is emitted each time the Gtk.LinkButton has been clicked.
The default handler will call Gtk.show_uri() with the URI stored inside the Gtk.LinkButton :uri property.
To override the default behavior, you can connect to the ::activate-link signal
and stop the propagation of the signal by returning True from your handler.
you need to install gvfs to get support for uri schemes such as http:// or ftp://,
as only local files are handled by GIO itself
"""
import gi
import webbrowser
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class LinkButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="LinkButton Demo")
self.set_border_width(10)
# button = Gtk.LinkButton(uri="file:///Users/xiaosanyu/mydocbook/docbook.html", label="Visit GTK+ Homepage")
button = Gtk.LinkButton(uri="http://www.gnome.org", label="Visit GTK+ Homepage")
button.connect("activate-link", self.visit)
self.add(button)
def visit(self, btn):
webbrowser.open(btn.get_uri())
return True
def main():
win = LinkButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
创建一个Gtk.LinkButton,绑定“activate-link”信号到visit方法
button = Gtk.LinkButton(uri="http://www.gnome.org", label="Visit GTK+ Homepage")
button.connect("activate-link", self.visit)
self.add(button)
def visit(self, btn):
webbrowser.open(btn.get_uri())
return True
当点击链接时,执行visit方法,使用Python自带webbrowser模块打开URI。并且返回True。
说明:默认点击时如果报错
Unable to show ‘http://www.gnome.org‘: 不支持该操作
需要安装“gvfs”
安装gvfs
jhbuild build gvfs
这个模块装起来比较麻烦,遇到各种各样的问题,最后装上后,点击链接依然报这个错,遂放弃。
如有在MAC上直接能打开此链接的同学,还请告知方法~~