python在tkinter打开网页_Python 2.7 Tkinter打开Web浏览器请点击

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值