How to create hyperlink in order to open new window from tkinter?

Question:

I want to create hyperlink in order to open new window. Is it possible to make hyperlink to open new window in tkinter? And if it’s possible, how to create it? Below this are my codes.

guidelines = Label(self, text="You can check the guidelines here", fg="blue", cursor="hand2")
guidelines.place(x=400,y=250)
guidelines.bind("<Button-1>", lambda e: callback())

Actually I’ve searched create hyperlink, but it’s only for opening a web page. And there are no references to open new window in tkinter

Asked By: Faustina Leonita

||

Answers:

You’ll need to create a Toplevel window. Tkinter can’t be compared to a browser, all content is inside some level of window of frame.

In your case, you want to have a new window, so this would be tkinter.Toplevel()

The Toplevel can be configured like the main window.

import tkinter as tk


def show_guide_lines(root):
    window_guide_lines = tk.Toplevel(root)
    window_guide_lines.title('Guide Lines')
    window_guide_lines.geometry('400x200')

    guide_line_text = '''
    Some Guildelines
    - 1
    - 2
    - 3
    '''
    tk.Label(window_guide_lines, text=guide_line_text).pack()

    window_guide_lines.tkraise()
    window_guide_lines.focus_force()


root = tk.Tk()
root.title('Main Window')
guidelines = tk.Label(root, text="You can check the guidelines here", fg="blue", cursor="hand2")
guidelines.pack(padx=20, pady=20)
guidelines.bind("<Button-1>", lambda e: show_guide_lines(root))

root.mainloop()
Answered By: Ovski

Change this:

guidelines.bind("<Button-1>", lambda e: callback())

to:

guidelines.bind("<Button-1>", lambda e: callback("https://stackoverflow.com/"))

You can select URL.

Answered By: toyota Supra
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.