python - TkInter: drawing on wrong positions -
i load picture on canvas. large picture need scroll vertically , horizontally able see it. let user drawn random curves/lines using mouse pointer on image.
everything ok except when scroll horizontally or vertically try draw see curves not drawn mouse points in other place. how can resolve problem ?
here code:
import pil.image import pil.imagetk tkinter import * import numpy np import cv2 class exampleapp(frame): def __init__(self,master): frame.__init__(self,master=none) self.x = self.y = 0 self.imcv=none self.canvas=canvas(self,width=600,height=600) self.b1="up" self.liste=[] self.xold=none self.yold=none def dessiner(self): # load imge , allow user scroll if large. self.canvas.bind("<motion>",self.motion) self.canvas.bind("<buttonpress-1>",self.b1down) self.canvas.bind("<buttonrelease-1>",self.b1up) self.sbarv=scrollbar(self,orient=vertical) self.sbarh=scrollbar(self,orient=horizontal) self.sbarv.config(command=self.canvas.yview) self.sbarh.config(command=self.canvas.xview) self.canvas.config(yscrollcommand=self.sbarv.set) self.canvas.config(xscrollcommand=self.sbarh.set) self.canvas.grid(row=0,column=0,sticky=n+s+e+w) self.sbarv.grid(row=0,column=1,stick=n+s) self.sbarh.grid(row=1,column=0,sticky=e+w) self.im = pil.image.open("image.jpg") self.widt,self.heigt=self.im.size self.canvas.config(scrollregion=(0,0,self.widt,self.heigt)) self.tk_im = pil.imagetk.photoimage(self.im) self.canvas.create_image(0,0,anchor="nw",image=self.tk_im) def b1up(self,event): self.b1="up" def b1down(self,event): self.b1="down" self.xold=none self.yold=none self.liste.append((self.xold,self.yold)) def motion(self,event): if self.b1=="down": if self.xold not none , self.yold not none: event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=true) self.xold=event.x self.yold=event.y self.liste.append((self.xold,self.yold)) if __name__ == "__main__": root=tk() app = exampleapp(root) app.pack() app.dessiner() root.mainloop()
if want see problem can download this large image , name image.jpg , run code.
may solution resides in using canvasx , canvasy not know how.
thank in advance
coordinates in event object in window coordinate system. need convert them canvas coordinate system. tkinter has 2 methods that: canvasx , canvasy.
Comments
Post a Comment