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

时间:2021-01-24 05:02:02

标签: python user-interface tkinter event-handling

我正在尝试制作一个应用程序,其中有一个按钮,单击该按钮将调用一个函数。我想将该功能放在主模块之外的另一个模块中。我把这个函数放在另一个模块中,然后导入到主模块中,但我不知道如何绑定。

基本上,我想知道如何访问由另一个主模块中定义的小部件定义在模块中的函数。例如,在给定的代码中,按钮在主模块中定义,但事件处理函数在另一个模块中定义。现在我想知道如何访问该函数 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.")

1 个答案:

答案 0 :(得分:1)

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)
相关问题