拖动Tkinter的窗口句柄? [英] Drag window handle for Tkinter?

查看:755
本文介绍了拖动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 ,事件):
self.x =无
self.y =无

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。几何(+%sx +%s%(self.x,self.y))

#root是我的窗口:
root = Tk()

#这是我如何分配类标签
WindowDraggable(label)

#All从Tkinter import *
导入
从PIL import Image,ImageTk
import sys
import re

我要完成的是使窗口可以由句柄拖动,在这种情况下 label 。我不能真正描述它现在的行为,但它确实会移动窗口,只是不按照鼠标。



请承担我,因为我是一个新来的人蟒蛇。任何帮助是赞赏:)一个重写的类是好的,我知道这是非常糟糕的写作。

解决方案

这里有一个例子:

$ b

 从Tkinter导入* 
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 =无
self.y =无

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 __ 中创建一个实例变量(除非你想要一个类变量,绝对不会在一个方法之外)。


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.

解决方案

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

这篇关于拖动Tkinter的窗口句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆