我有这个代码:
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()
理论上,该代码应该在lmb点击时打印鼠标坐标;createComponent";由于某种原因没有调用(也没有错误(。我做错了什么?
修复了代码:
- window.mainloop((已经是一个循环,将其放入
while True
会破坏代码 - 类设置错误
from tkinter import *
window = Tk()
def createComponent(event):
print("{0},{1}".format(event.x,event.y))
window.bind("<Button-1>", createComponent)
window.mainloop()
OOP:
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()
您可能希望将createComponent
放入class wind