Skip to content

Instantly share code, notes, and snippets.

@marvin
Created November 22, 2012 09:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marvin/4130284 to your computer and use it in GitHub Desktop.
Save marvin/4130284 to your computer and use it in GitHub Desktop.
tkinter with hyperlinks
@Jonius7
Copy link

Jonius7 commented Dec 19, 2020

After changing a few things to work with Python 3 (tkinter instead of Tkinter for starters), I found this works, but just doesn't work well.

Firstly, by default, after the first link in the message, it doesn't end the hyperlink tag, so text continues to have the underline and be part of the hyperlink. I changed the last "end", in tag_add, to "end-%dc" % 1, to get it to display correctly.

Secondly, if there's multiple links, the for loop just overrides all the previous links with the webbrowser.open(url) of the last url in the message. So all the links will just point to the same webpage.

I don't know how to fix this, so I ended up using another Hyperlink implementation here (adapted for Python 3): https://github.com/ChristianLowe/Grognaks-Mod-Manager/blob/master/lib/tkHyperlinkManager.py

This comment is just so anyone happening to stumble upon it in 2020 is aware of the state of this tkinter-hyperlinks.py and hopefully doesn't waste as much time as I did trying to get it to work.

@ShukhratSobich
Copy link

Hello there,

I had a problem with multiple links to.

It wont work also with the solution linked above, because the function works always with the same tag ("hyper"). In case of an iteration we have only one tag, that simply gets a new url each iteration. Different hyperlinks added in this way receive this one identical tag as well as the last url from a list.

The problem was already solved by martineau from stak overflow. So based on the solution I made a small function that works fine for me.

https://github.com/ShukhratSobich/python-functions/blob/main/hyperlink%20into%20tk.Text

I hope this information will help somebody.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment