使用overrideredirect时拖动窗口 [英] Drag window when using overrideredirect

查看:507
本文介绍了使用overrideredirect时拖动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 overrideredirect 从Tkinter窗口中删除边框,但是每当我这样做时,该窗口就会变得无响应。我不能使用 alt 并拖动它或任何其他方法来移动它。

我想制作一个看起来像那些加饭的应用程序之一的应用程序,光秃秃的窗户,很显然,如果它在左上角停滞不前的话,我走的不会很远。那么,我该怎么做呢?

I know how to remove a border from a Tkinter window using overrideredirect, but whenever I do that the window becomes unresponsive. I can't move it using alt and dragging, or any other method.
I want to make an application that looks like one of those "riced" applications that are just a bare window, and obviously I can't get very far if it just sits unresponsive in the upper-left corner. So, how do I do this?

推荐答案

要使窗口可拖动,请为放置绑定。窗口上的Button-1> (鼠标单击)和< B1-Motion> (鼠标移动)。

To make the window draggable, put bindings for <Button-1> (mouse clicks) and <B1-Motion> (mouse movements) on the window.

您需要做的就是存储鼠标按下事件的x和y值,然后在鼠标移动事件期间,根据当前的 pointer x和y,将原始事件 x和y隔开。

All you need to do is store the x and y values of a mouse down event and then during mouse motion events, you position the window based on the current pointer x and y, delta the original event x and y.

鼠标单击绑定的处理程序存储原始事件x和y。

The handler for the mouse click binding stores the original event x and y.

鼠标移动绑定的处理程序调用TopLevel方法 geometry()来根据当前鼠标位置和您从最近的鼠标单击中存储的偏移量。您为几何方法提供了 geometry字符串

The handler for the mouse movement binding calls the TopLevel method geometry() to reposition the window, based on current mouse position and the offset you have stored from the most recent mouse click. You supply a geometry string to the geometry method.

这里是一个非常小的示例,其中没有考虑到边缘屏幕上:

import tkinter

class Win(tkinter.Tk):

    def __init__(self,master=None):
        tkinter.Tk.__init__(self,master)
        self.overrideredirect(True)
        self._offsetx = 0
        self._offsety = 0
        self.bind('<Button-1>',self.clickwin)
        self.bind('<B1-Motion>',self.dragwin)

    def dragwin(self,event):
        x = self.winfo_pointerx() - self._offsetx
        y = self.winfo_pointery() - self._offsety
        self.geometry('+{x}+{y}'.format(x=x,y=y))

    def clickwin(self,event):
        self._offsetx = event.x
        self._offsety = event.y


win = Win()
win.mainloop()

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

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