拖动 Tkinter 的窗口句柄?
首先,这是我当前的代码,它的重要部分:
class WindowDraggable():
x = 1
y = 1
def __init__(self,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):
deltaX = event.x - self.x
deltaY = event.y - self.y
self.x = root.winfo_x() + deltaX
self.y = root.winfo_y() + deltaY
root.geometry("+%sx+%s" % (self.x,self.y))
#root is my window:
root = Tk()
#This is how I assign the class to label
WindowDraggable(label)
#All imports
from Tkinter import *
from PIL import Image, ImageTk
import sys
import re
我想要完成的是;使窗口可通过手柄拖动,在本例中为label
。我无法真正描述它现在的行为,但它确实移动了窗口,只是不跟随鼠标。
请耐心等待,因为我是 Python 的新手。任何帮助都是值得赞赏的:)重写该类是可以的,我知道它写得真的很糟糕。
First of all, this is my current code, essential parts of it:
class WindowDraggable():
x = 1
y = 1
def __init__(self,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):
deltaX = event.x - self.x
deltaY = event.y - self.y
self.x = root.winfo_x() + deltaX
self.y = root.winfo_y() + deltaY
root.geometry("+%sx+%s" % (self.x,self.y))
#root is my window:
root = Tk()
#This is how I assign the class to label
WindowDraggable(label)
#All imports
from Tkinter import *
from PIL import Image, ImageTk
import sys
import re
What I am trying to accomplish is; Make the window draggable by a handle, in this case label
. I can't really describe how it behaves now, but it does move the window, simply not following the mouse.
Please bear with me as I am a total newcomer to Python. Any help is appreciated :) A rewrite of the class is okay, I know it's really badly written.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个小例子:
您几乎是正确的,但您必须补偿标签本身内的偏移。请注意,我的示例没有补偿窗口边框。您将必须使用特定的工具来解决这个问题(因此,当使用
overrideredirect(1)
时,这个示例可以完美地工作。我猜您来自另一种编程语言,所以我会给您一些提示:
;
结束语句(虽然语法有效,但没有理由这样做)。look_like_this
。 > 或lookLikeThis
。Here's a little example:
You had it almost right, but you have to compensate for the offset within the label itself. Note that my example does not compensate for the window border. You will have to use of specific tools to figure that out (so this example works perfectly when using
overrideredirect(1)
.My guess is you're coming from another programming language, so I'll give you some tips while I'm at it:
;
(while valid syntax, there is no reason to do it).look_like_this
orlookLikeThis
.__init__
(and definitely not outside a method unless you want a class variable).