解决方法一:使用自动执行函数
def my_function():
print("函数被调用了")
# 自动执行函数
my_function()
解决方法二:使用装饰器
def my_decorator(func):
def wrapper():
print("函数被调用了")
func()
return wrapper
@my_decorator
def my_function():
print("函数被调用了")
# 函数被调用
my_function()
解决方法三:使用类和__init__方法
class MyClass:
def __init__(self):
print("函数被调用了")
# 实例化类时,函数被调用
my_instance = MyClass()
解决方法四:使用模块导入
# my_module.py
def my_function():
print("函数被调用了")
# main.py
import my_module
# 函数被调用
my_module.my_function()
下一篇:不需要按钮事件发送文件