拖放按钮tkinter python
问题描述
我是新使用的tkinter在python上,我想开发一个程序,可以拖放一个按钮按下另一个…我会试着解释:我有一个按钮‘A’,它将创建一个新的按钮‘B’,我想将这个新按钮拖到另一个地方 有什么帮助吗 谢谢
解决方案
tkinter.dnd
模块,如suggestedbyj_4321在评论中。
以下是使用库执行您所说操作一些示例代码:
from tkinter import *
from tkinter.dnd import Tester as DragWindow, Icon as Dragable
# Make a root window and hide it, since we don't need it.
root = Tk()
root.withdraw()
# Make the actual main window, which can have dragable objects on.
main = DragWindow(root)
def make_btn():
"""Make a new test button."""
# The functional part of the main window is the canvas.
Dragable('B').attach(main.canvas)
# Make a button and bind it to our button creating function.
Button(main.top, text='A', command=make_btn).pack()
# Start the mainloop.
mainloop()
相关文章