[python]

Asked 2 years ago, Updated 2 years ago, 106 views

from tkinter import *
import webbrowser

root = Tk()
root.title("URL")

# This is just a friend who opens up a browser window
def mkurl(event):
    webbrowser.open_new(event.widget.cget("text"))

#This is a function that gets the address bar input value
def ok():
    listbox.insert(0, txt.get())

#Input window to enter URL
txt = Entry(root)
btn = Button(root, text="OK", command = ok)
listbox = Listbox(root, selectmode ='extended', height=10)

listbox.grid(row=1,column=2)
txt.grid(row=0, column=1)
btn.grid(row=1, column=1)

root.mainloop()

Put url in the entry and press button to add it to the list. If you choose the added list, I want to add hyperlink function that opens the browser It won't go in. The traceback keeps coming up and going crazy even if I put the binding.

python3 python tkinter

2022-09-22 12:54

1 Answers

If you read the traceback carefully, you will find out the details of the error. And I can't program if I'm impatient.

There was no explanation of the error and no explanation of the code I wrote, so I roughly coded it.

You have registered the mkurl function as a handling function of the listbox ListboxSelect, and when you click on an element in the listbox, the browser runs to the registered address.

from tkinter import *
import webbrowser

root = Tk()
root.title("URL")

# This is just a friend who opens up a browser window
def mkurl(event):
    index = event.widget.curselection()
    webbrowser.open_new(event.widget.get(index))

#This is a function that gets the address bar input value
def ok():
    listbox.insert(0, txt.get())

#Input window to enter URL
txt = Entry(root)
btn = Button(root, text="OK", command = ok)
listbox = Listbox(root, selectmode ='extended', height=10)
listbox.bind('<<ListboxSelect>>', mkurl)

listbox.grid(row=1,column=2)
txt.grid(row=0, column=1)
btn.grid(row=1, column=1)

root.mainloop()


2022-09-22 12:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.