获取类/对象的所有属性和方法:dir()
dir(class)
dir(object)
eg:
print(dir(waigua))
result:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'crack_add_sun', 'crack_add_sun_r', 'crack_auto_collect', 'crack_auto_collect_r', 'crack_max_sun', 'crack_max_sun_r', 'crack_no_cd', 'crack_no_cd_r', 'crack_run_background', 'crack_run_background_r', 'crack_seckill', 'crack_seckill_r', 'get_all_crack', 'process_handle']
有了类名的字符串,创建对象,eval()
有了类的方法的字符串,调用方法,getattr()
class SayHello():
def say(self):
print(""hello)
if __name__ == '__main__':
# use string call class method
c_str = eval('SayHello')() # 通过eval来调用SayHello类
print(c_str)
c_str.say()
# direct use class method
getattr(c_str,'say')() # 通过getattr来调用对象的say方法
result:
<__main__.SayHello instance at 0x0000000002242848>
hello
hello
如果想将对象的方法传递给别的函数(类似于C语言中的函数指针),比如想将上述例子的c_str.say传递给别的函数,但是say方法代码中只能获取到字符串"say",那么这样做:
# 赋值
function_pointer = getattr(c_str,'say')
# 调用该函数/方法
function_pointer()
Tkinter Button按钮组件如何调用一个可以传入参数的函数
def fun(x):
print(x)
...
Button(root, text='Button', command=lambda :fun(x))
python tkinter button绑定多个函数
网上很多都是忘了那个:[ ]
command=lambda:[funcA(), funcB(), funcC()]
其他待研究的文章
tkinter: 多个Button绑定类似函数的简单写法
https://blog.youkuaiyun.com/weixin_50930712/article/details/108671847