如何用鼠标移动按钮 (tkinter)

问题描述 投票:0回答:1

标题基本上说明了一切。我想在窗口打开时用鼠标移动 tkinter 中的按钮。我该怎么做?? 运动不必精确...只是近似

完全不知道从哪里开始:(请帮忙

tkinter button
1个回答
0
投票

我想在窗口打开时用鼠标移动 tkinter 中的按钮 打开。我该怎么做??

使用

Button.bind

片段:

import tkinter as tk


class MoveButton:
    def __init__(self):
        self.button = tk.Button(root, text=f'move the button')
        self.button.place(x=0, y=0)
        self.button.bind("<Button-1>", self.pressed)
        self.button.bind("<ButtonRelease-1>", self.release)

    def pressed(self, e):
        self.button.bind("<Motion>", self.move)

    def release(self, e):
        self.button.unbind("<Motion>")

    def move(self, event: tk.Event):
        m_pos_x = root.winfo_pointerx() - root.winfo_rootx()
        m_pos_y = root.winfo_pointery() - root.winfo_rooty()
        self.button.place(x=m_pos_x, y=m_pos_y)


root = tk.Tk()
bl = MoveButton()
root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.