Python Tkinter里取回button 回调函数的变量
最近在尝试用用python的内置库tkinter写一个GUI应用,碰到了一些问题,这里note一下。
开发工具:pycharm
库:Tkinter
python 版本: python3.6
Button 组件介绍
button
在 Tkinter
里实现的是一个按钮功能。但是这里想实现取出command=func
中回调函数的变量值,因为在点击button
后
回调函数才开始被调用,但是语句self.button = Button(command = func)
执行后self.button 接收的是.!application.!button
,即使在
get_file_path
实例方法内添加return self.file_name
, 结果还是一样的,而且这个结果是在程序Run
后不需要点击button
就打印了
,即button
创建的时候就打印了。
Tips: 如果把command = func
改成command = print('hello world')
, 即把command=
后的函数名直接写成语句,那么button
不需要点击就会在创建的时候执行这个语句,所以一般可以在语句前加上 匿名函数lambda
, 这里是lambda: print('Hello world')
, Button
就会在点击后才打印Hello World
, 匿名函数实现了延迟调用的效果,这里具体怎么实现的还有待考究,如果知道的大神可以帮忙分析一波。
代码分析
先看一下代码
from tkinter import *
from tkinter.filed