views:

76

answers:

2

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

when I add this line of code (using the Python Shell with 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? The part that is giving problems says foreground="blue", which is known as a color in Tkinter, isn't it?

I am using Python 2.6 on MS Windows XP Pro

+1  A: 

"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...

Alex Martelli
I tried that, and it still presented the exact same error.
Zachary Brown
@Zachary, you mean you're giving `#00F` and it's **still** complaining about a `"blue"` that doesn't exist any more??? Or isn't that what you mean by "the exact same error"?
Alex Martelli
Probably because it isn't looking for a color at all but a `bitmap "blue" not defined` as you state first.
msw
Yes, it is still complaining about "blue", which doesn't exist any more.
Zachary Brown
Ok, but where is it looking for the bitmap? I went through all the code, but can't find anything that says it is a bitmap.
Zachary Brown
+3  A: 

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

class App:
    def __init__(self, root):
        self.root = root
        for text in ("link1", "link2", "link3"):
            link = Tkinter.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=Tkinter.Tk()
app = App(root)
root.mainloop()

If you want, you can get fancy and add addition bindings for <Enter> and <Leave> events so you can alter the look when users hover. And, of course, you can change the font so that the text is underlined if you so choose.

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.

Bryan Oakley