如何从另一个模块访问事件处理程序函数?



我试图使一个应用程序中,我有一个按钮,当点击将调用一个函数。我想把这个函数放在主模块之外的另一个模块中。我将函数放在另一个模块中,并将其导入主模块,但我不知道如何绑定它。

基本上,我想知道如何访问由在另一个主模块中定义的小部件在模块中定义的函数。例如,在给定的代码中,按钮在主模块中定义,但事件处理程序函数在另一个模块中定义。现在我想知道如何访问process_event函数。

MainModule.py

my_button = tk.Button(application_window, text="Example")
my_button.bind("<Enter>", process_event)

AmotherModule.py

def process_event(event):
print("The process_event function was called.")

MainModule.py

import AmotherModule 
my_button.bind("<Enter>", AmotherModule.process_event)

import AmotherModule as am
my_button.bind("<Enter>", am.process_event)

from AmotherModule import process_event
my_button.bind("<Enter>", process_event)

最新更新