任何关于如何创build事件绑定的build议,这些事件绑定将允许用户鼠标拖动无边框的窗口,例如。 一个用overridedirect(1)创build的窗口?
使用案例:我们想创build一个浮动的工具栏/调色板窗口(无边框),我们的用户可以在他们的桌面上拖动。
这就是我在想的地方(伪代码):
1. window.bind( '<Button-1>', onMouseDown ) to capture the initial position of the mouse. 2. window.bind( '<Motion-1>', onMouseMove ) to track position of mouse once it starts to move. 3. Calculate how much mouse has moved and calculate newX, newY positions. 4. Use window.geometry( '+%d+%d'% ( newX, newY ) ) to move window.
Tkinter是否公开了足够的function来允许我执行手头的任务? 还是有更容易/更高层次的方法来实现我想要做的事情?
是的,Tkinter公开了足够的功能来做到这一点,不,没有更简单/更高级别的方法来实现你想要做的事情。 你几乎有正确的想法。
这里是一个例子:
import Tkinter as tk import tkFileDialog class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.floater = FloatingWindow(self) class FloatingWindow(tk.Toplevel): def __init__(self, *args, **kwargs): tk.Toplevel.__init__(self, *args, **kwargs) self.overrideredirect(True) self.label = tk.Label(self, text="Click on the grip to move") self.grip = tk.Label(self, bitmap="gray25") self.grip.pack(side="left", fill="y") self.label.pack(side="right", fill="both", expand=True) self.grip.bind("<ButtonPress-1>", self.StartMove) self.grip.bind("<ButtonRelease-1>", self.StopMove) self.grip.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 x = self.winfo_x() + deltax y = self.winfo_y() + deltay self.geometry("+%s+%s" % (x, y)) app=App() app.mainloop()
试试这个,它肯定有效。
创建一个事件函数来移动窗口:
def movewindow(event):root.geometry('+ {0} + {1}'.format(event.x_root,event.y_root))
绑定窗口:
root.bind('',movewindow)
现在你可以触摸窗口并拖动