Home > Software engineering >  How to create hyperlink in order to open new window from tkinter?
How to create hyperlink in order to open new window from tkinter?

Time:10-26

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

CodePudding user response:

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

CodePudding user response:

Change this:

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

to:

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

You can select URL.

  • Related