|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lawrence1357 于 2021-2-4 21:27 编辑
from tkinter import *
class For_button:
def __init__(self, root):
self.root = root
for x in range(10) :
locals()['bnt'+str(x)]=Button(self.root, text=x)
locals()['bnt'+str(x)].grid()
locals()['bnt'+str(x)].config(command = lambda: self.fun_with_args(x))
def fun_with_args(self,x):
num=x
print('------------2-------------')
print(num)
if __name__=='__main__':
root=Tk()
For_button(root)
mainloop()
这个参数x每一次都传的是9,不应该是第一个button传0,第二个传1吗
- class For_button:
- def __init__(self, root):
- self.root = root
- for x in range(10) :
- locals()['bnt'+str(x)]=Button(self.root, text=x)
- locals()['bnt'+str(x)].grid()
- locals()['bnt'+str(x)].config(command = self.fun_with_args(x))
-
- def fun_with_args(self,x):
- def r():
- num=x
- print('------------2-------------')
- print(num)
- return r
复制代码
|
|