from tkinter import *
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from tkinter.filedialog import askdirectory
from threading import Thread
from pathlib import Path
from datetime import datetime
from humanize import naturalsize
class FileViewer(ttk.Frame):
def __init__(self,master):
super().__init__(master, padding=15)
self.pack(fill=BOTH,expand=YES)
self.pathentry = StringVar(value=Path().cwd())
self.createui()
self._insert_row()
def createui(self):
toplableframe = ttk.Labelframe(
self,
text="文件浏览器",
padding=(0,20,0,0)
)
toplableframe.pack(fill=X,expand=YES,anchor=N)
self.lfentry = ttk.Entry(
toplableframe,
textvariable=self.pathentry,
bootstyle="warning"
)
self.lfentry.pack(
fill=X,
side=LEFT,
expand=YES
)
rightbutton = ttk.Button(
master=toplableframe,
text="浏览",
command=self._rightbutton_click,
bootstyle="warning",
width=15
)
rightbutton.pack(
side=RIGHT,
)
self._create_tableview()
def _insert_row(self):
path = self.pathentry.get()
self.dt.delete_rows()
def _thread_func():
for i in Path(path).iterdir():
filename = i.stem
suffix = i.suffix.replace(".","")
stat = i.stat()
size = naturalsize(stat.st_size)
cgtime = stat.st_ctime
cgtime = datetime.fromtimestamp(cgtime).strftime('%Y-%m-%d %H:%M:%S')
abspath = i.absolute()
print(filename,suffix,size,cgtime,abspath)
if not suffix:
suffix = "-"
self.dt.insert_row(index=0, values=(filename, suffix, size, cgtime, abspath))
self.dt.load_table_data()
Thread(
target=_thread_func
).start()
def _rightbutton_click(self):
path = askdirectory()
if path:
self.pathentry.set(path)
self._insert_row()
def _create_tableview(self):
colors = app.style.colors
coldata = [
"文件名",
{'text':"类型",'width':100},
{'text':"大小",'width':150},
{'text':"修改时间",'width':260},
{'text':"绝对路径",'stretch':True}
]
rowdata = [
# ('A123', 'IzzyCo', 12,222222,2222222222),
# ('A136', 'Kimdee Inc.', 45,2222222222,2222222222222222222222),
# ('A158222222222', 'Farmadding Co.', 36,2222,2222222222222222222222222222222222222222222222222222222222222222222)
]
ttk.Separator(self,bootstyle="dark").pack(fill=X,pady=20)
self.dt = Tableview(
master=self,
coldata=coldata,
# rowdata=rowdata,
paginated=True,
searchable=False,
bootstyle="darkly",
stripecolor=(colors.secondary, None),
height=25,
pagesize=25,
)
self.dt.pack(fill=BOTH, expand=YES)
s = r"Smart - 8192 1622102435.9616566 C:\Users\LL\Desktop\CrystalDiskInfo8_12_0\Smart".split()
if __name__ == '__main__':
app = ttk.Window(
title="文件浏览器demo - Poweredby Jackma",
themename="darkly",
minsize=(1200,1000)
)
FileViewer(app)
app.place_window_center()
app.mainloop()
一个简单的文件浏览器demo基于tkinter
最新推荐文章于 2025-01-09 14:48:14 发布