Python 使用OverrideDirect时拖动窗口
我知道如何使用Python 使用OverrideDirect时拖动窗口,python,tkinter,draggable,Python,Tkinter,Draggable,我知道如何使用overrideredirect从Tkinter窗口中删除边框,但每当我这样做时,窗口就会变得无响应。我无法使用alt和拖动或任何其他方法移动它。 我想制作一个应用程序,它看起来像一个“RICE”应用程序,只是一个空窗口,显然,如果它在左上角没有响应,我就走不了多远。那么,我该怎么做呢?要使窗口可拖动,请在窗口上放置(鼠标单击)和(鼠标移动)的绑定 您只需存储鼠标按下事件的x和y值,然后在鼠标移动事件期间,根据当前指针x和y定位窗口,将原始事件x和y增量 鼠标单击绑定的处理程序存储
overrideredirect
从Tkinter窗口中删除边框,但每当我这样做时,窗口就会变得无响应。我无法使用alt和拖动或任何其他方法移动它。我想制作一个应用程序,它看起来像一个“RICE”应用程序,只是一个空窗口,显然,如果它在左上角没有响应,我就走不了多远。那么,我该怎么做呢?要使窗口可拖动,请在窗口上放置
(鼠标单击)和
(鼠标移动)的绑定
您只需存储鼠标按下事件的x和y值,然后在鼠标移动事件期间,根据当前指针x和y定位窗口,将原始事件x和y增量
鼠标单击绑定的处理程序存储原始事件x和y
鼠标移动绑定的处理程序调用顶级方法geometry()。可以向几何体方法提供几何体字符串
这里有一个不考虑屏幕边缘的非常简单的示例:
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()
导入tkinter
班级胜利(tkinter.Tk):
def uuu init uuu(self,master=None):
tkinter.Tk.\uuuuu初始化(self,master)
self.overrideredirect(True)
自补偿x=0
self.\u offsety=0
self.bind(“”,self.clickwin)
self.bind(“”,self.dragwin)
def dragwin(自身、事件):
x=self.winfo_pointerx()-self.\u offsetx
y=self.winfo\u pointery()-self.\u offsety
self.geometry('+{x}+{y}'。格式(x=x,y=y))
def clickwin(自我、事件):
self._offsetx=事件x
self.\u offsety=event.y
赢
win.mainloop()
要使窗口可拖动,请在窗口上放置
(鼠标单击)和
(鼠标移动)的绑定
您只需存储鼠标按下事件的x和y值,然后在鼠标移动事件期间,根据当前指针x和y定位窗口,将原始事件x和y增量
鼠标单击绑定的处理程序存储原始事件x和y
鼠标移动绑定的处理程序调用顶级方法geometry()。可以向几何体方法提供几何体字符串
这里有一个不考虑屏幕边缘的非常简单的示例:
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()
导入tkinter
班级胜利(tkinter.Tk):
def uuu init uuu(self,master=None):
tkinter.Tk.\uuuuu初始化(self,master)
self.overrideredirect(True)
自补偿x=0
self.\u offsety=0
self.bind(“”,self.clickwin)
self.bind(“”,self.dragwin)
def dragwin(自身、事件):
x=self.winfo_pointerx()-self.\u offsetx
y=self.winfo\u pointery()-self.\u offsety
self.geometry('+{x}+{y}'。格式(x=x,y=y))
def clickwin(自我、事件):
self._offsetx=事件x
self.\u offsety=event.y
赢
win.mainloop()
此解决方案适用于我:
from tkinter import *
import mouse
global x, y
def standard_bind():
root.bind('<B1-Motion>', lambda e: event(e, Mode=True))
def event(widget, Mode=False):
global x, y
if Mode:
x = widget.x
y = widget.y
root.bind('<B1-Motion>', lambda e: event(e))
root.geometry('+%d+%d' % (mouse.get_position()[0]-x, mouse.get_position()[1]-y))
root = Tk()
root.overrideredirect(True)
root.bind('<B1-Motion>', lambda e: event(e, Mode=True))
root.bind('<ButtonRelease-1>', lambda e: standard_bind())
root.geometry('%dx%d+%d+%d' % (600, 60, 50, 50))
mainloop()
从tkinter导入*
导入鼠标
全局x,y
def标准_绑定():
root.bind(“”,lambda e:event(e,Mode=True))
def事件(小部件,模式=False):
全局x,y
如果模式:
x=widget.x
y=widget.y
root.bind(“”,lambda e:event(e))
root.geometry(“++%d++%d%”(鼠标.获取位置()[0]-x,鼠标.获取位置()[1]-y))
root=Tk()
root.overrideredirect(True)
root.bind(“”,lambda e:event(e,Mode=True))
root.bind(“”,lambda e:standard_bind())
几何体(“%dx%d+%d+%d%”(600,60,50,50))
mainloop()
此解决方案适用于我:
from tkinter import *
import mouse
global x, y
def standard_bind():
root.bind('<B1-Motion>', lambda e: event(e, Mode=True))
def event(widget, Mode=False):
global x, y
if Mode:
x = widget.x
y = widget.y
root.bind('<B1-Motion>', lambda e: event(e))
root.geometry('+%d+%d' % (mouse.get_position()[0]-x, mouse.get_position()[1]-y))
root = Tk()
root.overrideredirect(True)
root.bind('<B1-Motion>', lambda e: event(e, Mode=True))
root.bind('<ButtonRelease-1>', lambda e: standard_bind())
root.geometry('%dx%d+%d+%d' % (600, 60, 50, 50))
mainloop()
从tkinter导入*
导入鼠标
全局x,y
def标准_绑定():
root.bind(“”,lambda e:event(e,Mode=True))
def事件(小部件,模式=False):
全局x,y
如果模式:
x=widget.x
y=widget.y
root.bind(“”,lambda e:event(e))
root.geometry(“++%d++%d%”(鼠标.获取位置()[0]-x,鼠标.获取位置()[1]-y))
root=Tk()
root.overrideredirect(True)
root.bind(“”,lambda e:event(e,Mode=True))
root.bind(“”,lambda e:standard_bind())
几何体(“%dx%d+%d+%d%”(600,60,50,50))
mainloop()
我刚刚解决了代码中窗口跳转的问题,请使用ButtonPress-1而不是Button-1,我的窗口仍在跳转-只有当我单击最右下角的边缘时它才不会跳转。我也遇到了这种情况,但我通过将行:self.\u offsetx=event.x
替换为self.\u offsetx=event.x+event.widget.winfo\u rootx()-self.winfo\u rootx()来修复它
并将self.\u offsety=event.y
替换为self.\u offsety=event.y+event.widget.winfo_rooty()-self.winfo_rooty()
我刚刚解决了代码中窗口跳转的问题,请使用ButtonPress-1而不是Button-1,我的窗口仍在跳转-只有当我单击最右下角的边缘时它才不会跳转。我也遇到了这种情况,但我通过将行:self.\u offsetx=event.x
替换为self.\u offsetx=event.x+event.widget.winfo\u rootx()-self.winfo\u rootx()来修复它
并将self.\u offsety=event.y
替换为self.\u offsety=event.y+event.widget.winfo_rooty()-self.winfo_rooty()
您应该描述代码的作用和原因,而不是只粘贴普通代码。您应该描述代码的作用和原因,而不是只粘贴普通代码。