这是我的类,它允许用户拖动画布。当我拖动它时,我的画布正在改变位置
class WindowDraggable():
def __init__(self, label):
self.label = label
label.bind('<ButtonPress-1>', self.StartMove)
label.bind('<ButtonRelease-1>', self.StopMove)
label.bind('<B1-Motion>', self.OnMotion)
def StartMove(self, event):
self.x = event.x
self.y = event.y
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self, event):
x = (event.x_root - self.x - self.label.winfo_rootx()
+ self.label.winfo_rootx())
y = (event.y_root - self.y - self.label.winfo_rooty()
+ self.label.winfo_rooty())
root.geometry("+%s+%s" % (x, y))
然而,当我点击并按住标签,随后的动作进行调整,以便将光标定位在画布的左上方的画布的位置。你可以看到光标在这个图像中的位置。
我想到了使用建议手动设置光标位置这样
def StartMove(self, event):
self.x = event.x + 100
self.y = event.y + 150
来解决这个问题,但问题是标签扩展到触摸屏的两侧,所以出于美学原因,光标应该简单地保持在其原始位置。我发现这个功能,认为它有助于解决问题,但我在使用它方面有点失落。
#sets mouse-cursor position
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
这是我缩小的程序,只显示与我的问题有关的基本部分。
from tkinter import *
from PIL import ImageTk, Image
#window creation
root = Tk()
root.overrideredirect(1) # eliminates frame around window
#canvas creation
canvas = Canvas(root, width=width, height=height, bd=-2)
canvas.pack()
#sets mouse-cursor position
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
#makes window draggable
class WindowDraggable():
def __init__(self, label):
self.label = label
label.bind('<ButtonPress-1>', self.StartMove)
label.bind('<ButtonRelease-1>', self.StopMove)
label.bind('<B1-Motion>', self.OnMotion)
def StartMove(self, event):
self.x = event.x + 100
self.y = event.y + 150
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self, event):
x = (event.x_root - self.x - self.label.winfo_rootx()
+ self.label.winfo_rootx())
y = (event.y_root - self.y - self.label.winfo_rooty()
+ self.label.winfo_rooty())
root.geometry("+%s+%s" % (x, y))
#creates the draggable label
label = Label(root, text='drag me')
WindowDraggable(label)
label.pack(fill=BOTH, expand=1)
#end of program
root.mainloop()
您的问题提到一个画布,但您的代码不显示画布。这只是一些代码直接从另一个SO答案复制和粘贴。你能给我们一个完整的例子来说明你的问题吗? – 2014-11-20 17:03:53
@BryanOakley好的,这是整个代码。我为什么被低估(再次)。 – user4261180 2014-11-20 17:06:56
我低估了你原来的问题,因为你提到一个问题而不显示任何研究工作。您发布的代码不是您的代码,而是从另一个帖子中剪切和粘贴的代码。你的问题提到了一个画布,但代码没有显示画布。所有这些都会造成质量问题。现在你有这个问题,你有太多的代码。执行ping的代码与移动窗口无关,为什么要包含它?你应该努力制定可能出现问题的最小程序。 – 2014-11-20 18:13:21