Tkinter 之文件管理器

github地址:https://github.com/yangsphp/file-manager-mask

一、效果图

  

 

 

 

 

 

 二、功能描述

1、打开文件菜单中的打开按钮,可以选择目录。

2、可以查看各种类型的图片。

3、可以编辑文本。

4、显示行号功能,可改变目录显示的宽度。

三、使用的标签

1、Menu

2、Frame

3、PanedWindow

4、Treeview

5、Text

6、Scrollbar

四、布局代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
__author__ = 'Yang'
 
from tkinter import *
from tkinter import ttk, messagebox, filedialog
import os
from PIL import Image, ImageTk
from threading import Thread
 
class Application_UI(object):
     
    # path = r"E:\\python开发工具\\project\\tkinter"
    path = os.path.abspath(".")
    file_types = [".png", ".jpg", ".jpeg", ".ico", ".gif"]
    scroll_visiblity = True
     
    font = 11
    font_type = "Courier New"
     
    def __init__(self):
        # 设置UI界面
        window = Tk()
        self.root = window
        win_width = 800
        win_height = 600
         
        screen_width, screen_height = window.maxsize()
        x = int((screen_width - win_width) / 2)
        y = int((screen_height - win_height) / 2)
        window.title("文件管理工具")
        window.geometry("%sx%s+%s+%s" % (win_width, win_height, x, y))
         
        menu = Menu(window)
        window.config(menu = menu)
         
        selct_path = Menu(menu, tearoff = 0)
        selct_path.add_command(label = "打开", accelerator="Ctrl + O", command = self.open_dir)
        selct_path.add_command(label = "保存", accelerator="Ctrl + S", command = self.save_file)
         
        menu.add_cascade(label = "文件", menu = selct_path)
         
        about = Menu(menu, tearoff = 0)
        about.add_command(label = "版本", accelerator = "v1.0.0")
        about.add_command(label = "作者", accelerator = "样子")
        menu.add_cascade(label = "关于", menu = about)
         
        # 顶部frame
        top_frame = Frame(window, bg = "#fff")
        top_frame.pack(side = TOP, fill = X)
        label = Label(top_frame, text = "当前选中路径:", bg = "#fff")
        label.pack(side = LEFT)
         
        self.path_var = StringVar()
        self.path_var.set("无")
        label_path = Label(top_frame, textvariable = self.path_var, bg = "#fff", fg = "red", height = 2)
        label_path.pack(anchor = W)
         
         
        paned_window = PanedWindow(window, showhandle = False, orient=HORIZONTAL)
        paned_window.pack(expand = 1, fill = BOTH)
         
        # 左侧frame
        self.left_frame = Frame(paned_window)
        paned_window.add(self.left_frame)
         
        self.tree = ttk.Treeview(self.left_frame, show = "tree", selectmode = "browse")
        tree_y_scroll_bar = Scrollbar(self.left_frame, command = self.tree.yview, relief = SUNKEN, width = 2)
        tree_y_scroll_bar.pack(side = RIGHT, fill = Y)
        self.tree.config(yscrollcommand = tree_y_scroll_bar.set)
        self.tree.pack(expand = 1, fill = BOTH)
         
                 
        # 右侧frame
        right_frame = Frame(paned_window)
        paned_window.add(right_frame)
         
        # 右上角frame
        right_top_frame = Frame(right_frame)
        right_top_frame.pack(expand = 1, fill = BOTH)
         
        self.number_line = Text(right_top_frame, width = 0, takefocus = 0, border = 0, font = (self.font_type, self.font), cursor = "")
        self.number_line.pack(side = LEFT, fill = Y)
         
         
        # 右上角Text
        text = Text(right_top_frame, font = (self.font_type, self.font), state = DISABLED, cursor = "", wrap = NONE)
        self.text_obj = text
        text_x_scroll = Scrollbar(right_frame, command = text.xview, orient = HORIZONTAL)
        text_y_scroll = Scrollbar(right_top_frame, command = text.yview)
        self.text_scroll_obj = text_y_scroll
        text.config(xscrollcommand = text_x_scroll.set, yscrollcommand = text_y_scroll.set)
        text_y_scroll.pack(side = RIGHT, fill = Y)
        text_x_scroll.pack(side = BOTTOM, fill = X)
        text.pack(expand = 1, fill = BOTH)
         
     
        # 右下角frame
        right_bottom_frame = Frame(right_frame)
        right_bottom_frame.pack(side = BOTTOM, fill = X)
         
        self.folder_img = PhotoImage(file = r"./image/folder.png")
        self.file_img = PhotoImage(file = r"./image/text_file.png")
         
        php_img = PhotoImage(file = r"./image/php.png")
        python_img = PhotoImage(file = r"./image/python.png")
        image_img = PhotoImage(file = r"./image/img.png")
         
         
        # 设置文件图标
        self.icon = {".php": php_img, ".py": python_img, ".pyc": python_img, ".png": image_img, ".jpg": image_img, ".jpeg": image_img, ".gif": image_img, ".ico": image_img}
         
        # 加载目录文件
        self.load_tree("", self.path)
        self.tree.bind("<<TreeviewSelect>>", lambda event: self.select_tree())
        text.bind("<MouseWheel>", lambda event : self.update_line())
         
        self.number_line.bind("<FocusIn>", self.focus_in_event)
        self.number_line.bind('<Button-1>', self.button_ignore)
        self.number_line.bind('<Button-2>', self.button_ignore)
        self.number_line.bind('<Button-3>', self.button_ignore)
        self.number_line.bind('<B1-Motion>', self.button_ignore)
        self.number_line.bind('<B2-Motion>', self.button_ignore)
        self.number_line.bind('<B3-Motion>', self.button_ignore)
         
        self.text_scroll_obj.bind('<B1-Motion>', lambda event: self.update_line())
        self.text_obj.bind('<KeyRelease>', lambda event: self.update_line())
         
        text.bind("<Control-Key-s>", lambda event: self.save_file())
        text.bind("<Control-Key-S>", lambda event: self.save_file())
        text.bind("<Control-Key-Z>", lambda event: self.toUndo())
        text.bind("<Control-Key-Y>", lambda event: self.toRedo())
         
        window.mainloop()

 

如果你感觉有收获,欢迎给我打赏 ———— 以激励我输出更多优质内容,联系QQ:2575404985
        
posted @   样子2018  阅读(3102)  评论(6编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示