鱼C论坛

 找回密码
 立即注册
查看: 2552|回复: 3

tkinter 如何绑字带参数的函数

[复制链接]
发表于 2015-2-12 15:28:36 | 显示全部楼层 |阅读模式

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

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

x
tkinter中如何绑定带参数的函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-2-13 13:57:35 | 显示全部楼层
终于弄清了:
from tkinter import *

class MyApp:
    def __init__(self, parent):
        self.myParent = parent
        self.myContainer1 = Frame(parent)
        self.myContainer1.pack()
        
        self.button1 = Button(self.myContainer1)
        self.button1.configure(text="OK", background= "green")
        self.button1.pack(side=LEFT)   
        self.button1.bind("<Button-1>", self.ff_adaptor(self.ff,x=10,y=20))
        
    def ff(self,event,x,y):
      
        s=x+y
        print(s)
    def ff_adaptor(self,fun,**kwds):
        return lambda event,fun=fun,kwds=kwds:fun(event,**kwds)
   
root = Tk()
myapp = MyApp(root)
root.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-13 13:58:19 | 显示全部楼层
用一个中介函数来帮助传递参数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-14 18:51:47 | 显示全部楼层
另一种方法:
from tkinter import *
class MyApp:
    def __init__(self,parent):
        self.myParent=parent
        self.myContainer=Frame(parent)
        self.myContainer.pack()
        self.btn=Button(self.myContainer)
        self.btn.configure(text="cal",background="green")
        self.btn.pack(side="left")
        self.btn.bind("<Button-1>",self.calc_do)
   
    def calc_do(self,event):
        self.calc(3,4,2)
    def calc(self,x,y,z):
        r=x**z+y**z
        print(r)
root=Tk()
myapp=MyApp(root)
root.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-14 21:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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