-
Is there a way to add clickable hyperlinks inside a |
Beta Was this translation helpful? Give feedback.
Answered by
Akascape
Mar 29, 2024
Replies: 1 comment 1 reply
-
@invader276 Yes it's possible to add clickable hyperlinks inside import customtkinter
import webbrowser
class CTkHyperlinkManager:
"""
Modified class for implementing hyperlink in CTkTextbox
"""
def __init__(self,
master,
text_color="#82c7ff"):
self.text = master
self.text.tag_config("hyper", foreground=text_color, underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.links = {}
def add(self, link):
tag = "hyper-%d" % len(self.links)
self.links[tag] = link
return "hyper", tag
def _enter(self, event):
self.text.configure(cursor="hand2")
def _leave(self, event):
self.text.configure(cursor="xterm")
def _click(self, event):
for tag in self.text.tag_names(customtkinter.CURRENT):
if tag[:6] == "hyper-":
webbrowser.open(self.links[tag])
return
# EXAMPLE USAGE
root = customtkinter.CTk()
textbox = customtkinter.CTkTextbox(root)
textbox.insert("end", "Click this link: ")
textbox.pack(padx=10, pady=10)
# Real implementation here
hyperlink = CTkHyperlinkManager(textbox)
textbox.insert("end", "github.com", hyperlink.add("https://www.github.com"))
textbox.insert("end", "\nGoogle.com", hyperlink.add("https://www.google.com"))
root.mainloop() |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
w11-z
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@invader276 Yes it's possible to add clickable hyperlinks inside
CTkTextBox
.Here is the implementation: