timport ...
mdx=-1 , mdy=-1
class mouse_data:
def on_click(x, y, button, pressed):
global mdx , global mdy
if pressed and button == mouse.Button.middle:
print("middle mouseclick: " + "X:" + str(x) + " y: "+ str(y))
mdx = x , mdy = y
Toplevel1.show_mousepos("") #invoke function from Toplevel1 class
#---start pynput Listener -----------------------------
mouselistener = mouse.Listener(on_click = on_click)
mouselistener.start()
class Toplevel1:
def show_mousepos(self): #>to write mdx, mdy in class Toplevel1 label['text']
print("mdx:"+ str(mdx) + " mdy:"+str(mdy)) #--ok > mdx and mdy as globals are seen
self.lbl2['text'] = str(mdy)
#>gives--AttributeError: 'str' object has no attribute 'lbl2'
#-or
self.lbl2.configure(text = str(mdy))
#>gives--AttributeError: 'str' object has no attribute 'lbl2'
#?????--Why does this function not recognize self.label as in btnxy_click ??????
"-------------------------------------------------------------------------------
def btnxy_click(self):
self.lbl2['text'] = "XY CLICK LABEL2"
def btnstop_click(self):
exit()
def __init__(self, top=None):
#..This class configures and populates the toplevel window as top.
#..all that is needed to build the frame and widgets...
self.top = top
self.lbl2 = tk.Label(self.top)
self.lbl2.configure(text='''Label2''')
self.btnXY = tk.Button(self.top)
self.btnXY.configure(text='''XY''')
self.btnXY.configure(command = self.btnxy_click)
self.btnStop = tk.Button(self.top)
self.btnStop.configure(text="STOP")
self.btnStop.configure(command = self.btnstop_click)
def start_up():
BRC_ASSISTCURSOR_TEST2_support.main()
if __name__ == '__main__':
BRC_ASSISTCURSOR_TEST2_support.main()
我只留下了必要的行,使用 PAGE 来构建 GUI。 运行正常,gui 顶部框架出现,btnxy_click 更改 lbl2 文本。 鼠标中键单击起作用并在编辑器中使用打印命令显示 x,y 坐标。
但是我没有找到一种方法可以在 lbl2 标签中显示这些坐标
作为一个新手,我不明白发生了什么,尽管功能和标签 属于同一个 Toplevel1 类。
感谢您的帮助和指导。
请检查
show_mousepos
中的缩进。这两行 self.lbl2 ...
行在您的帖子中缺少一级,因此位于功能之外,这可能可以解释问题。