How to create a hyperlink with a Label in Tkinter?

后端 未结 4 1040
逝去的感伤
逝去的感伤 2020-12-01 14:21

How do you create a hyperlink using a Label in Tkinter?

A quick search did not reveal how to do this. Instead there were only solutions

4条回答
  •  广开言路
    2020-12-01 14:42

    Bind the label to "" event. When it is raised the callback is executed resulting in a new page opening in your default browser.

    from tkinter import *
    import webbrowser
    
    def callback(url):
        webbrowser.open_new(url)
    
    root = Tk()
    link1 = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
    link1.pack()
    link1.bind("", lambda e: callback("http://www.google.com"))
    
    link2 = Label(root, text="Ecosia Hyperlink", fg="blue", cursor="hand2")
    link2.pack()
    link2.bind("", lambda e: callback("http://www.ecosia.org"))
    
    root.mainloop()
    

    You can also open files by changing the callback to:

    webbrowser.open_new(r"file://c:\test\test.csv")
    

提交回复
热议问题