url - Python creating dynamic button -


i have array, holds urls.

i want create button, within tk, opens url in browser.

so far, i've got button create, however, when run .py file, seems function open page being hit - not prepared button.

import tkinter tk import webbrowser web urls = [[1, 'http://www.google.co.uk','google'],[2, 'http://www.bbc.co.uk','bbc']];  def urlopen(target):     web.open(target)  window = tk.tk() window.overrideredirect(1) window.update_idletasks()      url in urls:     urltarget = url[0]     labelurl = tk.button(window, text=url[2], height = 2, command = urlopen(url[1]), width = 10)     labelurl.grid(row = url[0] + 1 , column = 1)  close = tk.button(window, text = 'quit',  command = window.destroy, width = 10) close.grid(row = len(urls) + 2, column = 1) window.mainloop() 

i know, using oop python easier - i'm new this, , still picking things up!

what's happening you're calling function when create button -- not when gets clicked:

labelurl = tk.button(window, text=url[2], height = 2, command = urlopen(url[1]), width = 10) 

what need defer function call:

for url in urls:     onclick = lambda url=url[1]: urlopen(url)     labelurl = tk.button(window, text=url[2], height = 2, command = onclick, width = 10)     labelurl.grid(row = url[0] + 1, column = 1) 

here i've created new function each url -- when called no arguments, function call urlopen correct url. (note i'm using standard lambda x=y: x trick make sure lambda function picks correct url in loop).


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -