TkInter:绘制错误的位置 [英] TkInter: drawing on wrong positions
问题描述
我在 Canvas 上加载了一张图片.这是一张大图片,所以我需要垂直和水平滚动才能看到它.我还让用户在图像上使用鼠标指针绘制随机曲线/线条.
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 ?
这是我的代码:
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()
如果你想看看我的问题,那么你可以下载这个 大图像并将其命名为 image.jpg 并运行代码.
If you want to see my problem then you can download this large image and name it image.jpg and run the code.
可能解决方案在于使用 canvasx 和 canvasy 但我不知道怎么做.
May be the solution resides in using canvasx and canvasy but I do not know how.
提前致谢
推荐答案
我通过更改以下几行解决了该问题:
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)
和:
self.xold=event.x
self.yold=event.y
到:
event.widget.create_line(self.xold,self.yold,self.canvas.canvasx(event.x),self.canvas.canvasy(event.y),fill="red",width=3,smooth=TRUE)
和:
self.xold=self.canvas.canvasx(event.x)
self.yold=self.canvas.canvasy(event.y)
这篇关于TkInter:绘制错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!