Python 当事件发生时,如何分配变量的值?

Python 当事件发生时,如何分配变量的值?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,在Tkinter中,当事件发生时,如何分配变量的值 from Tkinter import * def on_Click(event): print(event.x, event.y) return 'hello world !' root = Tk() root.bind("<Button-1>", on_Click) myVariable = on_Click() # rest of the program root.mainloop() 从Tkinter导

在Tkinter中,当事件发生时,如何分配变量的值

from Tkinter import *

def on_Click(event):
    print(event.x, event.y)
    return 'hello world !'

root = Tk()
root.bind("<Button-1>", on_Click)
myVariable = on_Click()
# rest of the program
root.mainloop()
从Tkinter导入*
单击时的def(事件):
打印(事件x、事件y)
返回“你好,世界!”
root=Tk()
root.bind(“,单击时)
myVariable=单击时()
#节目的其余部分
root.mainloop()
当触发(通过单击小部件)单击
时,如何将
myVariable
的值分配给
on\u Click()
的值


只有成功地将值分配给
myVariable
时,才会执行程序的其余部分。在没有记录单击的情况下阻止程序。

问题是,启动脚本时整个程序都会运行,函数中不包含的所有内容都会在开始时加载,以构建UI。。。。而真正的工作是在加载函数时开始的,比如您编写的函数(
点击

您需要做的是在函数中引用
myVariable
,然后将变量发送到另一个函数,该函数将是程序的其余部分

from Tkinter import *

variable = None

def on_Click(event):
    print(event.x, event.y)
    variable = 'hello world !'
    main(variable)

root = Tk()
root.bind("<Button-1>", on_Click)

def main(x):
# rest of the program

root.mainloop()
从Tkinter导入*
变量=无
单击时的def(事件):
打印(事件x、事件y)
变量=‘你好,世界!’
主(变量)
root=Tk()
root.bind(“,单击时)
def干管(x):
#节目的其余部分
root.mainloop()
这就是我让它工作的方式,别忘了每次你要点击小部件时,程序都会再次加载