Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

python - Clickable Tkinter labels

I will be populating a frame with a list of labels representing URLs. The url's will be fed in from a list and can be between 3 and 5 in number, decided by user. What is the easiest way to make these url's clickable, so the user can get to the website displayed? Is there a better way to do this than use labels?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Labels are fine I think. You just need to bind a callback to a mouse click.

def open_url(url):
    pass #Open the url in a browser

for i,url in enumerate(url_list):
    label=tk.Label(frame,text=url)
    label.grid(row=i)
    label.bind("<Button-1>",lambda e,url=url:open_url(url))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...