提供用鼠标拖动、缩放tkinter控件工具的跨平台模块。A cross-platform module providing tools to dragand resize tkinter windows and widgets with the mouse.
Project description
- NAME
tk_dragtool
- DESCRIPTION 简介
提供用鼠标拖动、缩放tkinter控件工具的模块。 A module providing tools to drag and resize tkinter window and widgets with the mouse.
- FUNCTIONS 函数
- bind_drag(tkwidget, dragger)
绑定拖曳事件。 tkwidget: 被拖动的控件或窗口, dragger: 接收鼠标事件的控件, 调用bind_drag后,当鼠标拖动dragger时, tkwidget会被带着拖动, 但dragger 作为接收鼠标事件的控件, 位置不会改变。 x 和 y: 作用同上。
- bind_resize(tkwidget, dragger, anchor, min_w=0, min_h=0, move_dragger=True)
绑定缩放事件。 anchor: 缩放的方位, 取值为N,S,W,E,NW,NE,SW,SE,分别表示东、西、南、北。 min_w,min_h: 该方向tkwidget缩放的最小宽度(或高度)。 move_dragger: 缩放时是否移动dragger。 其他说明同bind_drag函数。
- draggable(tkwidget)
调用draggable(tkwidget) 使tkwidget可拖动。 tkwidget: 一个控件(Widget)或一个窗口(Wm)。 x 和 y: 只允许改变x坐标或y坐标。
- getpos()
获取鼠标当前位置。
- move(widget, x=None, y=None, width=None, height=None)
移动控件或窗口widget, 参数皆可选。
EXAMPLES 示例
import tkinter as tk
from tk_dragtool import draggable
root=tk.Tk()
btn=tk.Button(root,text="Drag")
draggable(btn)
btn.place(x=0,y=0)
root.mainloop()
运行效果:
更复杂的示例, 实现了8个缩放手柄的功能:
btns=[] # 用btns列表存储创建的按钮
def add_button(func,anchor):
# func的作用是计算按钮新坐标
b=ttk.Button(root)
b._func=func
bind_resize(btn,b,anchor)
x,y=func()
b.place(x=x,y=y,width=size,height=size)
b.bind('<B1-Motion>',adjust_button,add='+')
b.bind('<B1-ButtonRelease>',adjust_button,add='+')
btns.append(b)
def adjust_button(event=None):
# 改变大小或拖动后,调整手柄位置
for b in btns:
x,y=b._func()
b.place(x=x,y=y)
root=tk.Tk()
root.title("Test")
root.geometry('500x350')
btn=ttk.Button(root,text="Button")
draggable(btn)
btn.bind('<B1-Motion>',adjust_button,add='+')
btn.bind('<B1-ButtonRelease>',adjust_button,add='+')
x1=20;y1=20;x2=220;y2=170;size=10
btn.place(x=x1,y=y1,width=x2-x1,height=y2-y1)
root.update()
# 创建各个手柄, 这里是控件缩放的算法
add_button(lambda:(btn.winfo_x()-size, btn.winfo_y()-size),
'nw')
add_button(lambda:(btn.winfo_x()+btn.winfo_width()//2,
btn.winfo_y()-size), 'n')
add_button(lambda:(btn.winfo_x()+btn.winfo_width(), btn.winfo_y()-size),
'ne')
add_button(lambda:(btn.winfo_x()+btn.winfo_width(),
btn.winfo_y()+btn.winfo_height()//2),'e')
add_button(lambda:(btn.winfo_x()+btn.winfo_width(),
btn.winfo_y()+btn.winfo_height()), 'se')
add_button(lambda:(btn.winfo_x()+btn.winfo_width()//2,
btn.winfo_y()+btn.winfo_height()),'s')
add_button(lambda:(btn.winfo_x()-size, btn.winfo_y()+btn.winfo_height()),
'sw')
add_button(lambda:(btn.winfo_x()-size,
btn.winfo_y()+btn.winfo_height()//2), 'w')
root.mainloop()
效果图:
版本:1.1.4 (更新: 修复了对Linux等系统的支持问题)
作者:七分诚意 qq:3076711200
作者CSDN主页: https://blog.csdn.net/qfcy_/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.