windows Tkinter Text 小部件中的超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3402110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hyperlink in Tkinter Text widget?
提问by Zac Brown
I am re designing a portion of my current software project, and want to use hyperlinksinstead 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:
我正在重新设计我当前软件项目的一部分,并且想要使用超链接而不是Buttons
. 我真的不想使用Text
小部件,但这就是我在谷歌搜索这个主题时所能找到的。无论如何,我找到了一个这样的例子,但不断收到这个错误:
TclError: bitmap "blue" not defined
When I add this line of code (using the IDLE
)
当我添加这行代码时(使用IDLE
)
hyperlink = tkHyperlinkManager.HyperlinkManager(text)
The code for the module is located hereand the code for the script is located here
Anyone have any ideas?
谁有想法?
The part that is giving problems says foreground="blue"
, which is known as a color in Tkinter, isn't it?
出现问题的部分说foreground="blue"
,在 Tkinter 中被称为颜色,不是吗?
回答by Bryan Oakley
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.
如果您不想使用文本小部件,则不需要。另一种方法是使用标签并将鼠标点击绑定到它。即使它是一个标签,它仍然对事件做出响应。
For example:
例如:
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()
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.
如果您愿意,您可以花哨并为<Enter>
和<Leave>
事件添加额外的绑定,以便您可以在用户悬停时更改外观。而且,当然,如果您愿意,您可以更改字体,以便文本带有下划线。
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.
Tk 是一个很棒的工具包,它为您提供了可以做任何您想做的事情的构建块。您只需要将小部件视为一组预制的墙壁和门,而不是像一堆腰椎、砖块和砂浆。
回答by Alex Martelli
"blue"
should indeed be acceptable (since you're on Windows, Tkinter should use its built-in color names table -- it might be a system misconfiguration on X11, but not on Windows); therefore, this is a puzzling problem (maybe a Tkinter misconfig...?). What happen if you use foreground="#00F"
instead, for example? This doesn't explain the problem but might let you work around it, at least...
"blue"
确实应该是可以接受的(因为你在 Windows 上,Tkinter 应该使用它的内置颜色名称表——它可能是 X11 上的系统配置错误,但不是在 Windows 上);因此,这是一个令人费解的问题(可能是 Tkinter 配置错误...?)。foreground="#00F"
例如,如果您改为使用会发生什么?这并不能解释问题,但至少可以让您解决它...