import tkinter
"""
tk 可拖动窗口Demo
双击关闭程序
"""
class DragWindow:
def __init__(self):
self.root = tkinter.Tk()
self.x, self.y = 0, 0
self.window_size = '300x200'
self.root.overrideredirect(True)
self.root.attributes("-alpha", 0.4)
self.root.geometry(f"{self.window_size}+10+10")
self.root.configure(bg="blue")
self.root.bind("", self.move)
self.root.bind("", self.get_point)
self.root.bind("", self.close)
def move(self, event):
"""窗口移动事件"""
new_x = (event.x - self.x) + self.root.winfo_x()
new_y = (event.y - self.y) + self.root.winfo_y()
s = f"{self.window_size}+{new_x}+{new_y}"
self.root.geometry(s)
def get_point(self, event):
"""获取当前窗口位置并保存"""
self.x, self.y = event.x, event.y
def run(self):
self.root.mainloop()
def close(self, event):
self.root.destroy()
if __name__ == "__main__":
window = DragWindow()
window.run()