简体   繁体   English

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

[英]How to add a hyperlink to a tkinter button

I currently working on some code on Tkinter and I want to know if its possibly and if so how to add a website hyperlink to a button.我目前正在 Tkinter 上编写一些代码,我想知道它是否可能以及如何将网站超链接添加到按钮。 In my case I'm trying to add the Caldicot School web address to a button through Tkinter on Python 3 and when its clicked it sends you there在我的例子中,我试图通过 Tkinter 在 Python 3 上将 Caldicot School web 地址添加到一个按钮,当它被点击时,它会把你送到那里

Welcome to SO! 欢迎来到SO!

This page has a recipe for creating a button that acts like a hyperlink in tkinter http://code.activestate.com/recipes/580774-tkinter-link-or-hyperlink-button/ 此页面包含创建按钮的方法,该按钮的作用类似于tkinter中的超链接http://code.activestate.com/recipes/580774-tkinter-link-or-hyperlink-button/

The main part of the code is as follows: 该代码的主要部分如下:

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

Check the website above for the code behind the class Link_Button . 在上面的网站上查看类Link_Button后面的代码。 In case the link dies, here's the rest of the code: 万一链接消失,下面是其余的代码:

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

You can basicly add this method:您基本上可以添加此方法:

from tkinter import *
from tkinter import ttk
import webbrowser

root = Tk()
root.title = 'Link Button'
def link():
    webbrowser.open_new(r"https://www.python.org")

and then link method to the button:然后将方法链接到按钮:

nut = ttk.Button(root, text='Link Button', command=link)
nut.pack()
root.mainloop()
import tkinter
import webbrowser

root = Tk()
root.title = 'link to the button'
def link():
    webbrowser.open_new(r"https://www.python.org")

nut = ttk.Button(root, text='link to the button')
nut.pack()
root.mainloop() 

and then just simply use然后只需简单地使用

nut = ttk.Button(root, text= 'link to the button', command=link)
nut.pack()
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM