Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Tkinter绑定事件不是';t调用函数_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x Tkinter绑定事件不是';t调用函数

Python 3.x Tkinter绑定事件不是';t调用函数,python-3.x,tkinter,Python 3.x,Tkinter,我有以下代码: from tkinter import * class logic(): def createComponent(self, event): print("{0},{1}".format(event.x,event.y)) class gui(logic): window = Tk() obj = logic() def initGui(self): gui.window.mainl

我有以下代码:

    from tkinter import *

class logic():
    def createComponent(self, event):
        print("{0},{1}".format(event.x,event.y))

class gui(logic):
    window = Tk()
    obj = logic()

    def initGui(self):
        gui.window.mainloop()

    def onClick(self):
        gui.window.bind("<Button-1>",gui.obj.createComponent)

obj2 = gui()
obj2.initGui()
while True:
    obj2.onClick()
从tkinter导入*
类逻辑():
def createComponent(自身、事件):
打印(“{0},{1}”。格式(event.x,event.y))
类gui(逻辑):
window=Tk()
obj=逻辑()
def initGui(self):
gui.window.mainloop()
def onClick(自):
gui.window.bind(“,gui.obj.createComponent)
obj2=gui()
obj2.initGui()
尽管如此:
obj2.onClick()
理论上,这段代码应该在lmb点击时打印鼠标坐标,但由于某种原因没有调用“createComponent”(也没有错误)。我做错了什么?

修复了代码:

  • window.mainloop()已经是一个循环,将其放入
    ,而True则会中断代码
  • 课程设置错误
从tkinter导入*
window=Tk()
def createComponent(事件):
打印(“{0},{1}”。格式(event.x,event.y))
window.bind(“,createComponent)
window.mainloop()
面向对象:

从tkinter导入*
类windFuncs:
def createComponent(事件):
打印(“{0},{1}”。格式(event.x,event.y))
风力等级(Tk):
通过
窗=风()
window.bind(“,windFuncs.createComponent)
window.mainloop()

您可能希望将
createComponent
放入
classwind

bind
中传递事件的位置绑定自身将“事件”传递给createComponent移除while循环,并从
initGui
调用
onclick
,确保将其称为b4 mainloop。谢谢,这是重现问题所需的最少代码,gui和逻辑类包含一系列其他方法。我添加了OOP,非常感谢您的欢迎__
from tkinter import *

window = Tk()

def createComponent(event):
    print("{0},{1}".format(event.x,event.y))

window.bind("<Button-1>", createComponent)

window.mainloop()
from tkinter import *


class windFuncs:
    def createComponent(event):
        print("{0},{1}".format(event.x,event.y))

class wind(Tk):
    pass
    

window = wind()

window.bind("<Button-1>", windFuncs.createComponent)

window.mainloop()