拖动 Tkinter 的窗口句柄?

发布于 2024-12-05 11:34:51 字数 1035 浏览 0 评论 0原文

首先,这是我当前的代码,它的重要部分:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-12-12 11:34:51

这是一个小例子:

from Tkinter import *
root = Tk()

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))

label = Label(root, text='drag me')
WindowDraggable(label)
label.pack()
root.mainloop()

您几乎是正确的,但您必须补偿标签本身内的偏移。请注意,我的示例没有补偿窗口边框。您将必须使用特定的工具来解决这个问题(因此,当使用overrideredirect(1)时,这个示例可以完美地工作。

我猜您来自另一种编程语言,所以我会给您一些提示:

  • Python 不会以 ; 结束语句(虽然语法有效,但没有理由这样做)。
  • 方法名称应该一致 look_like_this。 > 或 lookLikeThis
  • 变量不需要声明。如果您想创建实例变量,请在 __init__ 中进行声明(并且绝对不要在方法外部声明,除非您需要类变量)。

Here's a little example:

from Tkinter import *
root = Tk()

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))

label = Label(root, text='drag me')
WindowDraggable(label)
label.pack()
root.mainloop()

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:

  • Python does not end statements with a ; (while valid syntax, there is no reason to do it).
  • Method names should either consistently look_like_this or lookLikeThis.
  • Variables do not need to be declared. If you want to create an instance variable do so in __init__ (and definitely not outside a method unless you want a class variable).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文