Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TkInter:在错误的位置绘制_Python_Python 2.7_Tkinter - Fatal编程技术网

Python TkInter:在错误的位置绘制

Python TkInter:在错误的位置绘制,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我把一幅画放在画布上。这是一个大图片,所以我需要垂直和水平滚动才能看到它。我还允许用户在图像上使用鼠标指针绘制随机曲线/线 一切正常,只是当我水平或垂直滚动时,我尝试绘制曲线,我看到曲线不是在鼠标指向的地方绘制的,而是在另一个地方绘制的。我如何解决这个问题 这是我的密码: import PIL.Image import PIL.ImageTk from Tkinter import * import numpy as np import cv2 class ExampleApp(

我把一幅画放在画布上。这是一个大图片,所以我需要垂直和水平滚动才能看到它。我还允许用户在图像上使用鼠标指针绘制随机曲线/线

一切正常,只是当我水平或垂直滚动时,我尝试绘制曲线,我看到曲线不是在鼠标指向的地方绘制的,而是在另一个地方绘制的。我如何解决这个问题

这是我的密码:

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()
导入PIL.Image
导入PIL.ImageTk
从Tkinter进口*
将numpy作为np导入
进口cv2
类示例应用程序(框架):
定义初始(自我,主):
帧。\uuuu初始化(self,master=None)
self.x=self.y=0
self.imcv=None
self.canvas=canvas(self,宽度=600,高度=600)
self.b1=“向上”
self.liste=[]
self.xold=None
self.yold=None
def干燥器(自身):
#加载imge并允许用户滚动它(如果它很大)。
self.canvas.bind(“,self.motion)
self.canvas.bind(“,self.b1down)
self.canvas.bind(“,self.b1up)
self.sbarv=滚动条(self,orient=垂直)
self.sbarh=滚动条(self,orient=水平)
self.sbarv.config(命令=self.canvas.yview)
self.sbarh.config(命令=self.canvas.xview)
self.canvas.config(yscrollcommand=self.sbarv.set)
self.canvas.config(xscrollcommand=self.sbarh.set)
self.canvas.grid(行=0,列=0,粘性=N+S+E+W)
self.sbarv.grid(行=0,列=1,棒=N+S)
self.sbarh.grid(行=1,列=0,粘性=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.b1=“向上”
def b1down(自身、事件):
self.b1=“向下”
self.xold=None
self.yold=None
self.liste.append((self.xold、self.yold))
def运动(自身、事件):
如果self.b1==“向下”:
如果self.xold不是None,self.yold不是None:
event.widget.create_行(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))
如果名称=“\uuuuu main\uuuuuuuu”:
root=Tk()
app=ExampleApp(root)
app.pack()
app.dessiner()
root.mainloop()
如果您想查看我的问题,那么可以下载大图像并将其命名为image.jpg,然后运行代码

可能解决方案在于使用和,但我不知道如何使用


提前感谢

事件对象中的坐标位于窗口坐标系中。您需要将它们转换为画布坐标系。Tkinter有两种方法:和

我通过更改以下行解决了问题:

event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=TRUE)
以及:

致:

以及:


非常感谢。正如我在问题中所说,我知道
canvasx
canvasy
但是当我应用它们时,我得到了奇怪的结果,所以我不知道结果在哪里problem@Nakkini:我们不可能不看到您的代码就知道您为什么会得到“奇怪的结果”。你能做的最好的事情就是创建一个。这道题很可能归结为一道简单的数学题。
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)