我想将回调连接到标签文本
例如在以下代码中
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
def hello(*args):
print "hello, this is most used text editor for pyton"
def wellcome(*args):
print "wellcome to the our program please update to premium version"
w = gtk.Window(title = "example", type = gtk.WindowType.TOPLEVEL)
w.resize(300, 200)
mylabel = gtk.Label()
mylabel.set_markup("""please read """
"""<span underline = "single" command = "hello">hello</span> or """
"""<span underline = "single" command = "wellcome">wellcome</span>""")
w.add(mylabel)
w.show_all()
gtk.main()
我知道,pango span属性不包含命令选项,好的。还有另一种方法吗?
答案 0 :(得分:1)
您可以通过将处理程序连接到activate-link
信号来实现此目的。请注意,回调的签名与按钮单击不同,您应该返回True以停止进一步处理。
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
label = Gtk.Label()
label.set_markup('<a href="mylink">Click Here</a> but not here.')
label.connect("activate-link", self.on_link_clicked)
self.add(label)
def on_link_clicked(self, label, uri):
print("%s clicked" % uri)
return True
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
答案 1 :(得分:0)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
label = Gtk.Label()
label.set_markup('please read <a href="hello">Hello</a> or <a href="wellcome">wellcome</a>')
label.connect("activate-link", self.on_link_clicked)
self.add(label)
def hello(self):
print "hello, this is most used text editor for pyton"
def wellcome(self):
print "wellcome to the our program please update to premium version"
def on_link_clicked(self, label, uri):
print("%s clicked" % uri)
if uri == "hello":
self.hello()
elif uri == "wellcome":
self.wellcome()
return True
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()