HTML links in a GUI tkinter

Eric Brunel eric.brunel at pragmadev.com
Mon Jun 3 17:23:47 CEST 2002


Steve Holden wrote:
> So, all Aurel needs to know now is how to run wxPython widgets inside
> tkinter programs...

Why? Tkinter has everything to do it:

----------------------------------
from Tkinter import *

root = Tk()
t = Text(root)
t.pack()

def openHLink(event):
  start, end = t.tag_prevrange("hlink",
                               t.index("@%s,%s" % (event.x, event.y)))
  print "Going to %s..." % t.get(start, end)

t.tag_configure("hlink", foreground='blue', underline=1)
t.tag_bind("hlink", "<Control-Button-1>", openHLink)
t.insert(END, "This is a link\n")
t.insert(END, "http://www.python.org", "hlink")
t.insert(END, "\nAnd text goes on...\n")
root.mainloop()
----------------------------------

Control-click on the link and you should see the message "Going to <link 
text>", which may be replaced by a webbrowser.open to actually open the 
URL. To insert a new link, just do:
t.insert(<pos>, <url>, "hlink")

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list