34-鼠标拖拽文件功能

鼠标拖拽文件功能

直接拖动部分文件或者文件夹到tkinter窗口,实现快速添加文件功能。

需要安装windnd库。

demo如下:

import tkinter as tk
import windnd
from tkinter.messagebox import showinfo
 
 
def dragged_files(files):
    msg = '\n'.join((item.decode('gbk') for item in files))
    showinfo("拖拽文件路径",msg)
 
if __name__ == '__main__':
    rootWindow = tk.Tk()
    windnd.hook_dropfiles(rootWindow , func=dragged_files)
    rootWindow .mainloop()

例子如下:

例1:活用lambda在处理方法传入多个函数.

from tkinter import Tk, BROWSE
from tkinter.ttk import Treeview
from windnd import hook_dropfiles


def dragged_files(files, files_show):
    for idx, file_path in enumerate(files):
        file_path = file_path.decode('gbk')
        inster_value = [idx, file_path, ""]
        files_show.insert('', index=idx, values=inster_value)


if __name__ == '__main__':
    # 主窗口
    root = Tk()

    # 窗口标题
    root.title("拖拽文件更新列表")

    # 桌面长宽
    win_w = root.winfo_screenwidth()
    win_h = root.winfo_screenheight()

    # 窗口长宽
    width = 550
    height = 450

    # 设置窗口位置和长宽
    root.geometry(f"{width}x{height}+{(win_w - width) // 2}+{(win_h - height) // 2}")

    # 不可调整大小
    root.resizable(False, False)

    # 窗口置顶
    root.wm_attributes('-topmost', 1)

    # 树状标签
    columns = ("idx", "file_path", "status")
    file_tree = Treeview(root, show="headings", columns=columns, selectmode=BROWSE)
    file_tree.place(relx=0, rely=0, relwidth=1, relheight=1)

    file_tree.column("idx", anchor="w", width=50)  # 设置表格文字靠左
    file_tree.column("file_path", anchor="w", width=400)
    file_tree.column("status", anchor="w", width=100)

    file_tree.heading("idx", text="序号")  # 设置表格头部标题
    file_tree.heading("file_path", text="文件地址")
    file_tree.heading("status", text="状态")

    hook_dropfiles(root, func=lambda paths: dragged_files(paths, file_tree))
    root.mainloop()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

士别三日,当挖目相待

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值