Welcome, guest | Sign In | My Account | Store | Cart
# Author: Miguel Martinez Lopez

try:
    from Tkinter import Label
    from ttk import Style
    from tkFont import Font
except ImportError:
    from tkinter import Label
    from tkinter.ttk import Style
    from tkinter.font import Font

class Link_Button(Label, object):
    def __init__(self, master, text, background=None, font=None, familiy=None, size=None, underline=True, visited_fg = "#551A8B", normal_fg = "#0000EE", visited=False, action=None):
        self._visited_fg = visited_fg
        self._normal_fg = normal_fg
        
        if visited:
            fg = self._visited_fg
        else:
            fg = self._normal_fg
            
        if font is None:
            kw = {"family":"TkDefaultFont", "underline":underline}
            if size:
                kw["size"] = size
            font = Font(**kw)

        Label.__init__(self, master, text=text, fg=fg, cursor="hand2", font=font)

        if background is None:
            # If background is None, we get background of parent window
            parent_name = self.winfo_parent()
            widget = self.nametowidget(parent_name)

            try:
                background = widget.cget("background")
            except:
                style = widget.cget("style")
                if style == "":
                    style = widget.winfo_class()
                background = Style().lookup(style, 'background')

        self.configure(background=background)

        self._visited = visited
        self._action = action

        self.bind("<Button-1>", self._on_click)

    @property
    def visited(self):
        return self._visited
        
    @visited.setter
    def visited(self, is_visited):
        if is_visited:
            self.configure(fg=self._visited_fg)
            self._visited = True
        else:
            self.configure(fg=self._normal_fg)
            self._visited = False

    def _on_click(self, event):
        if not self._visited:
            self.configure(fg=self._visited_fg)

        self._visited = True

        if self._action:
            self._action()


if __name__ == "__main__":
    import webbrowser

    try:
        from Tkinter import Tk, Frame
    except ImportError:
        from tkinter import Tk, Frame    

    def callback():
        webbrowser.open_new(r"http://www.google.com")

    root = Tk()
    frame = Frame(root, bg="white")
    frame.pack(expand=True, fill="both")

    link = Link_Button(frame, text="Google Hyperlink", action=callback)
    link.pack(padx=10, pady=10)
    root.mainloop()

Diff to Previous Revision

--- revision 1 2017-04-04 12:08:37
+++ revision 2 2017-04-04 12:20:35
@@ -36,6 +36,8 @@
                 background = widget.cget("background")
             except:
                 style = widget.cget("style")
+                if style == "":
+                    style = widget.winfo_class()
                 background = Style().lookup(style, 'background')
 
         self.configure(background=background)
@@ -74,14 +76,13 @@
     try:
         from Tkinter import Tk, Frame
     except ImportError:
-        from tkinter import Tk, Frame
+        from tkinter import Tk, Frame    
 
     def callback():
         webbrowser.open_new(r"http://www.google.com")
 
     root = Tk()
-    
-    frame = Frame(root, background="white")
+    frame = Frame(root, bg="white")
     frame.pack(expand=True, fill="both")
 
     link = Link_Button(frame, text="Google Hyperlink", action=callback)

History