Tkinter Text小部件中的超链接? [英] Hyperlink in Tkinter Text widget?
问题描述
我正在设计当前软件项目的一部分,并希望使用超链接而不是按钮
。我真的不想使用 Text
小部件,但这是我搜索主题时能找到的全部内容。无论如何,我找到了一个这样的例子,但不断收到此错误:
I am re designing a portion of my current software project, and want to use hyperlinks instead of Buttons
. I really didn't want to use a Text
widget, but that is all I could find when I googled the subject. Anyway, I found an example of this, but keep getting this error:
TclError: bitmap "blue" not defined
当我添加这行代码时(使用 IDLE
)
When I add this line of code (using the IDLE
)
hyperlink = tkHyperlinkManager.HyperlinkManager(text)
The code for the module is located here and the code for the script is located here
任何人都有任何想法?
Anyone have any ideas?
提出问题的部分是 foreground =blue
,这在Tkinter中被称为颜色,不是吗?
The part that is giving problems says foreground="blue"
, which is known as a color in Tkinter, isn't it?
推荐答案
如果您不想使用文本小部件,则不需要。另一种方法是使用标签并将鼠标点击绑定到它。即使它是一个标签,它仍然会响应事件。
If you don't want to use a text widget, you don't need to. An alternative is to use a label and bind mouse clicks to it. Even though it's a label it still responds to events.
例如:
import tkinter as tk
class App:
def __init__(self, root):
self.root = root
for text in ("link1", "link2", "link3"):
link = tk.Label(text=text, foreground="#0000ff")
link.bind("<1>", lambda event, text=text: self.click_link(event, text))
link.pack()
def click_link(self, event, text):
print("You clicked '%s'" % text)
root = tk.Tk()
app = App(root)
root.mainloop()
如果需要,您可以为< Enter>
添加额外的绑定<离开>
事件,以便您可以在用户悬停时更改外观。当然,如果您愿意,可以更改字体以便文本加下划线。
If you want, you can get fancy and add additional bindings for <Enter>
and <Leave>
events so you can alter the look when the user hovers. And, of course, you can change the font so that the text is underlined if you so choose.
Tk是一个很棒的工具包,可以为您提供构建基块关于你想要什么。您只需将小部件看作一组预制的墙壁和门,但更像是一堆腰,砖和砂浆。
Tk is a wonderful toolkit that gives you the building blocks to do just about whatever you want. You just need to look at the widgets not as a set of pre-made walls and doors but more like a pile of lumbar, bricks and mortar.
这篇关于Tkinter Text小部件中的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!