类似cad里的长十字光标
昨天试了华泰的K线,鼠标进入的时候会画一个长的十字光标,类似cad里的那种。鼠标退出时光标消失。按左方向K线往以前的移动,右方向往新的移动,上方向则放大K线,向下则缩小K线。
本来认为这个难度也不大,但是用canvas绑定鼠标进入退出的回调函数,结果怎么试都没有回应。绑到类上也是没反应。生气之下,就用窗口先试一下这些回调。结果是可以回调的,但是为什么绑定到canvas上就回调不成功呢?
在窗口里回调先画一个十字线做测试。
本想把原来的十字光标去掉,但是没试成功。
#!/usr/bin/env python
# -*- coding:gb2312 -*-
from tkinter import *
class App:
def __init__(self):
self.root = Tk()
self.root.title('十字线测试...')
self.cvs = Canvas(self.root, width=800, height=600, bg='black', cursor='crosshair')
self.cvs.pack(fill=BOTH, expand=1)
self.root.resizable(0,0)
self.root.bind('<Enter>', self.do_Enter)
self.root.bind('<Leave>', self.do_Leave)
self.root.bind('<Motion>', self.do_Enter)
self.cur_xx = None
self.cur_yy = None
self.cur_txt = None
def do_Enter(self, event):
if self.cur_xx is None:
self.cur_xx = self.cvs.create_line(0, event.y, 799, event.y, fill='green')
else:
self.cvs.coords(self.cur_xx, 0, event.y, 799, event.y)
if self.cur_yy is None:
self.cur_yy = self.cvs.create_line(event.x, 0, event.x, 599, fill='green')
else:
self.cvs.coords(self.cur_yy, event.x, 0, event.x, 599)
if self.cur_txt is None:
self.cur_txt = self.cvs.create_text(event.x, event.y, anchor=SW, text='X%d,Y%d' % (event.x,event.y), fill='green')
else:
self.cvs.coords(self.cur_txt, event.x, event.y)
self.cvs.itemconfig(self.cur_txt, text='X%d,Y%d' % (event.x,event.y) )
def do_Leave(self, event):
if self.cur_xx is not None:
self.cvs.delete(self.cur_xx)
self.cur_xx = None
if self.cur_yy is not None:
self.cvs.delete(self.cur_yy)
self.cur_yy = None
if self.cur_txt is not None:
self.cvs.delete(self.cur_txt)
self.cur_txt = None
def Run(self):
self.root.mainloop()
app = App()
app.Run()