问题
I load a picture on the Canvas. It is a large picture so I need to scroll vertically and horizontally to be able to see it. I also let the user to drawn random curves/lines using the mouse pointer on the image.
Everything is ok except that when I scroll horizontally or vertically then I try to draw I see the curves are not drawn where the mouse points to but in an other place. How can I resolve this problem ?
Here is my code:
import PIL.Image
import PIL.ImageTk
from Tkinter import *
import numpy as 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 the imge and allow user to scroll it if it is 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 is not None and self.yold is 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 you want to see my problem then you can download this large image and name it image.jpg and run the code.
May be the solution resides in using canvasx and canvasy but I do not know how.
Thank you in advance
回答1:
Coordinates in the event object are in the window coordinate system. You need to convert them to the canvas coordinate system. Tkinter has two methods for that: canvasx and canvasy.
回答2:
I resolved the problem by changing these lines:
event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=TRUE)
and:
self.xold=event.x
self.yold=event.y
to:
event.widget.create_line(self.xold,self.yold,self.canvas.canvasx(event.x),self.canvas.canvasy(event.y),fill="red",width=3,smooth=TRUE)
and:
self.xold=self.canvas.canvasx(event.x)
self.yold=self.canvas.canvasy(event.y)
来源:https://stackoverflow.com/questions/30256715/tkinter-drawing-on-wrong-positions