如何将超链接添加到tkinter按钮

时间:2018-10-18 20:52:33

标签: python tkinter

我目前正在Tkinter上编写一些代码,我想知道是否可能以及如何将网站超链接添加到按钮。就我而言,我正在尝试通过Python 3上的Tkinter将Caldicot School网址添加到按钮上,并在单击时将您发送到该地址

1 个答案:

答案 0 :(得分:2)

欢迎您!

此页面包含创建按钮的方法,该按钮的作用类似于tkinter中的超链接 http://code.activestate.com/recipes/580774-tkinter-link-or-hyperlink-button/

代码的主要部分如下:

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

    # Creates a button that, when clicked, calls the function that sends you to your hyperlink.
    link = Link_Button(frame, text="Google Hyperlink", action=callback)
    link.pack(padx=10, pady=10)
    root.mainloop()

在上面的网站上查看类Link_Button后面的代码。万一链接消失,这是其余的代码:

# Author: Miguel Martinez Lopez

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

def get_background_of_widget(widget):
    try:
        # We assume first tk widget
        background = widget.cget("background")
    except:
        # Otherwise this is a ttk widget
        style = widget.cget("style")

        if style == "":
            # if there is not style configuration option, default style is the same than widget class
            style = widget.winfo_class()

        background = Style().lookup(style, 'background')

    return background

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:
            default_font = nametofont("TkDefaultFont")
            family = default_font.cget("family")

            if size is None:
                size = default_font.cget("size")

            font = Font(family=family, size=size, underline=underline)

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

        if background is None:
            background = get_background_of_widget(master)

        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()