34-鼠标拖拽文件功能

鼠标拖拽文件功能

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

需要安装windnd库。

demo如下:

import tkinter as tk
import windnd
from tkinter.messagebox import showinfodef 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_dropfilesdef 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 = 550height = 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()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部