from Tkinter import *
import webbrowser
root = Tk()
frame = Frame(root)
frame.pack()
url = 'http://www.sampleurl.com'
def OpenUrl(url):
webbrowser.open_new(url)
button = Button(frame, text="CLICK", command=OpenUrl(url))
button.pack()
root.mainloop()
我的目标是在单击GUI小部件中的按钮时打开一个URL.但是我不是
确定该怎么做.当我不单击而运行脚本时,Python将打开两个新窗口
任何东西.另外,当我单击按钮时,什么也没有发生.
解决方法:
你应该用
button = Button(root, text="CLCK", command=lambda aurl=url:OpenUrl(aurl))
这是需要参数时发送回调的正确方法.
从here开始:
A common beginner’s mistake is to call the callback function when
constructing the widget. That is, instead of giving just the
function’s name (e.g. “callback”), the programmer adds parentheses and
argument values to the function:
If