如何在 tcl tk 中创建超链接?
How to create an hyperlink in tcl tk?
我正在使用 Tcl-Tk 8.6.4,我是初学者,我想在我的文本中创建超链接。
我正在寻找一个可以在参数中包含网站 url 的程序,此 url 将以蓝色显示并在我的文本中加下划线。当然,通过点击 url,它会打开网站。
我找到了以下代码,但我不确定它是否会执行我想要的操作。
proc hyperlink { name args } {
if { "Underline-Font" ni [ font names ] } {
font create Underline-Font {*}[ font actual TkDefaultFont ]
font configure Underline-Font -underline true -size 12
}
if { [ dict exists $args -command ] } {
set command [ dict get $args -command ]
dict unset args -command
}
label $name {*}$args -foreground blue -font Underline-Font
if { [ info exists command ] } {
bind $name <Button-1> $command
}
return $name
}
谁能帮帮我?
更新 2
我想要的是在我的 window 文本中显示超链接,就像这样:
更多信息在这些链接后给出:
- https://ccrma.stanford.edu/~jos/NumericalInt/Lumped_vs_Distributed_Systems.html
- https://ccrma.stanford.edu/~jos/NumericalInt/NumericalInt_4up.pdf
显示第一个代码后,我可以显示:
使用的代码是:
在读取文件的过程中我有像HTML这样的标签 像
这样的标签
"<hypLink>" {
gets $infile inln
hyperlink .hl${counter} -command [list eval exec [auto_execok start] "$inln"] -text "$inln"
pack .hl${counter}
incr counter
}
在我的文件中我写
半导体:
<hypLink>
https://en.wikipedia.org/wiki/Semiconductor
<hypLink>
http://electronics.howstuffworks.com/diode.htm
我该怎么做才能得到我想要的?
注意第一次更新因版权保护已延迟
您可以使用找到的 proc
。如果你有:
hyperlink .hl -command [list puts "clicked"] -text "Click Me"
pack .hl
在您的代码中单击 'hyperlink',您将得到文本 clicked
到标准输出。
如果要打开默认浏览器并转到 hyperlink 指定的 url,则必须将代码更改为:
hyperlink .hl -command [list eval exec [auto_execok start] "http://www.example.com"] -text "Click Me"
pack .hl
您也可以尝试一下 proc,也许可以更改字体大小(从 -size 12
开始)或更改字体本身。
Post编辑:
您可以通过将以下内容添加到 dispFile
过程中的开关来将 'hyperlink' 添加到您的代码中:
"<hypLink>" {
gets $infile inln
.fr.txt insert end "$inln" "link lk$lkcount"
.fr.txt insert end "\n" Normal
.fr.txt tag bind lk$lkcount <1> [list eval exec [auto_execok start] "$inln"]
incr lkcount
}
这应该会创建类似于打开文件的文本,但不会打开文件,而是会在默认浏览器中打开 link。
我正在使用 Tcl-Tk 8.6.4,我是初学者,我想在我的文本中创建超链接。 我正在寻找一个可以在参数中包含网站 url 的程序,此 url 将以蓝色显示并在我的文本中加下划线。当然,通过点击 url,它会打开网站。 我找到了以下代码,但我不确定它是否会执行我想要的操作。
proc hyperlink { name args } {
if { "Underline-Font" ni [ font names ] } {
font create Underline-Font {*}[ font actual TkDefaultFont ]
font configure Underline-Font -underline true -size 12
}
if { [ dict exists $args -command ] } {
set command [ dict get $args -command ]
dict unset args -command
}
label $name {*}$args -foreground blue -font Underline-Font
if { [ info exists command ] } {
bind $name <Button-1> $command
}
return $name
}
谁能帮帮我?
更新 2
我想要的是在我的 window 文本中显示超链接,就像这样:
更多信息在这些链接后给出:
- https://ccrma.stanford.edu/~jos/NumericalInt/Lumped_vs_Distributed_Systems.html
- https://ccrma.stanford.edu/~jos/NumericalInt/NumericalInt_4up.pdf
显示第一个代码后,我可以显示:
在读取文件的过程中我有像HTML这样的标签 像
这样的标签"<hypLink>" { gets $infile inln hyperlink .hl${counter} -command [list eval exec [auto_execok start] "$inln"] -text "$inln" pack .hl${counter} incr counter }
在我的文件中我写
半导体:
<hypLink> https://en.wikipedia.org/wiki/Semiconductor <hypLink> http://electronics.howstuffworks.com/diode.htm
我该怎么做才能得到我想要的?
注意第一次更新因版权保护已延迟
您可以使用找到的 proc
。如果你有:
hyperlink .hl -command [list puts "clicked"] -text "Click Me"
pack .hl
在您的代码中单击 'hyperlink',您将得到文本 clicked
到标准输出。
如果要打开默认浏览器并转到 hyperlink 指定的 url,则必须将代码更改为:
hyperlink .hl -command [list eval exec [auto_execok start] "http://www.example.com"] -text "Click Me"
pack .hl
您也可以尝试一下 proc,也许可以更改字体大小(从 -size 12
开始)或更改字体本身。
Post编辑:
您可以通过将以下内容添加到 dispFile
过程中的开关来将 'hyperlink' 添加到您的代码中:
"<hypLink>" {
gets $infile inln
.fr.txt insert end "$inln" "link lk$lkcount"
.fr.txt insert end "\n" Normal
.fr.txt tag bind lk$lkcount <1> [list eval exec [auto_execok start] "$inln"]
incr lkcount
}
这应该会创建类似于打开文件的文本,但不会打开文件,而是会在默认浏览器中打开 link。