鱼C论坛

 找回密码
 立即注册
查看: 2009|回复: 1

[已解决]请问tkinter中绑定事件函数怎么传参

[复制链接]
发表于 2022-7-27 12:52:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在tkinter中定义了事件,然后绑定事件时调用函数,此时函数需要一个event参数,但是如果我还想往这个函数里传参,系统就要我定义event,请问此时我该怎么传参,代码如下
import tkinter as tk
from tkinter import *


def open_url(url,event):
    webbrowser.open(url, new=0)

window = tk.Tk()
window.geometry('400x400')
tk.Frame(window,relief='groove',bd = 1).pack()

new = tk.Label(window,text = f' No.1 ',
                            width = 25,height =1)
new.place(x=50,y=50,anchor=NW)
new.bind("<Button-1>", open_url(url = 'https://tophub.today/'))     
    
window.mainloop() 
最佳答案
2022-7-28 13:15:24
使用lambda匿名函数
在把15行改成new.bind("<Button-1>", lambda event: open_url(url = 'https://tophub.today/', event=event))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-28 13:15:24 | 显示全部楼层    本楼为最佳答案   
使用lambda匿名函数
在把15行改成new.bind("<Button-1>", lambda event: open_url(url = 'https://tophub.today/', event=event))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表