#-*- coding:utf-8 -*-
#
import tkinter
import tkinter.messagebox
import os,os.path
import threading
rubbishExt = [
'.tmp',
'.bak',
'.old',
'.wbk',
'.xlk',
'._mp',
'.log',
'.gid',
'.chk',
'.syd',
'.$$$',
'.@@@',
'.~*']
class Window:
# def GetDrives(self):
# drives = []
# for i in range(65, 91):
# vol = chr(i) + ':/'
# if os.path.isdir(vol):
# drives.append(vol)
# return tuple(drives)
def ScanRubbish(self):
global rubbishExt
total =
0
filesize =
0
for root,dirs,files,
in os.walk(
'c:/'):
try :
for fil
in files:
filesplit =
os.path.splitext(fil)
if filesplit[1] ==
'':
continue
try:
if rubbishExt.index(filesplit[1])>=
0:
fname =
os.path.join(os.path.abspath(root),fil)
filesize +=
os.path.getsize(fname)
if total % 15 ==
0:
self.flist.delete(0.0
,tkinter.END)
self.flist.insert(tkinter.END,fname+
'\n')
l =
len(fname)
if l>50
:
self.progress['text']=fname[:25]+
'...'+fname[1-25
:l]
else:
self.progress['text'] =
fname
total+=1
except ValueError:
pass
except Exception as e:
print(e)
pass
self.progress['text'] =
"找到%s个垃圾文件,共占用%.2f M磁盘空间"%(total,filesize/1024/1024
)
def DelteRubbish(self):
global rubbishExt
total =
0
filesize =
0
for root,dirs,files
in os.walk(
'c:/'):
try:
for fil
in files:
filesplit =
os.path.splitext(fil)
if filesplit[1]==
'':
continue
try:
if rubbishExt.index(filesplit[1])>=
0:
fname =
os.path.join(os.path.abspath(root),fil)
filesize+=
os.path.getsize(fname)
try:
os.remove(fname)
l =
len(fname)
if l >50
:
fname = fname[:25]+
'...'+fname[1-25
:l]
if total %15 ==
0:
self.flist.delete(0.0
,tkinter.END)
self.flist.insert(tkinter.END,'Deleted'+fname+
'\n')
self.progress['text']=
fname
total +=1
except:
pass
except ValueError:
pass
except Exception as e:
print(e)
pass
def __init__(self):
self.root =
tkinter.Tk()
#创建菜单
menu =
tkinter.Menu(self.root)
#创建System子菜单
submenu = tkinter.Menu(menu,tearoff=
0)
submenu.add_command(label =
'About..',command =
self.MenuAbout)
submenu.add_separator()
submenu.add_command(label =
'Exit',command =
self.MenuExit)
menu.add_cascade(label =
'System',menu =
submenu)
#创建clean子菜单
submenu = tkinter.Menu(menu,tearoff=
0)
submenu.add_command(label =
'scan',command =
self.MenuScanRubbish)
submenu.add_command(label =
'Del',command =
self.MenuDelRubbish)
menu.add_cascade(label =
'Clean',menu =
submenu)
#创建Search子菜单
submenu =tkinter.Menu(menu,tearoff=
0)
submenu.add_command(label =
'SearchBigData',command =
self.MenuScanBigFile)
submenu.add_separator()
submenu.add_command(label =
'SearchName',command =
self.MenuSearchFile)
menu.add_cascade(label =
'Search',menu =
submenu)
self.root.config(menu =
menu)
self.progress = tkinter.Label(self.root,anchor =
tkinter.W,
text =
'状态',bitmap =
'hourglass',compound=
'left')
self.progress.place(x=10,y=370,width=480,height=15
)
self.flist =
tkinter.Text(self.root)
self.flist.place(x=10,y=10,width = 480,height = 350
)
self.vscroll =
tkinter.Scrollbar(self.root)
self.vscroll.pack(side =
'right',fill =
'y')
self.flist['yscrollcommand'] =
self.vscroll.set
self.vscroll['command']=
self.flist.yview
def MainLoop(self):
self.root.title("Findfat")
self.root.minsize(500,400
)
self.root.maxsize(500,400
)
self.root.mainloop()
def MenuAbout(self):
tkinter.messagebox.showinfo("Findfat",
"这是python编写的window优化程序\n欢迎提出意见")
def MenuExit(self):
self.root.quit()
def MenuScanRubbish(self):
result = tkinter.messagebox.askquestion(
"Findfat",
"扫描垃圾文件时间较长,是否继续?")
if result ==
'no':
return
tkinter.messagebox.showinfo("Findfat",
"马上删除")
#self.ScanRubbish()
t = threading.Thread(target=
self.ScanRubbish)
t.start()
def MenuDelRubbish(self):
result = tkinter.messagebox.askquestion(
"Findfat",
"删除")
if result ==
'no':
return
tkinter.messagebox.showinfo("Findfat",
"马上删除")
t= threading.Thread(target=
self.DelteRubbish)
t.start()
def MenuScanBigFile(self):
result = tkinter.messagebox.askquestion(
"Findfat",
"搜索时间")
if result ==
'no':
return
tkinter.messagebox.showinfo("Findfat",
"马上搜索")
def MenuSearchFile(self):
result = tkinter.messagebox.askquestion(
"Finafat",
"文件名时间")
if result ==
'no':
return
tkinter.messagebox.showinfo("Findfat",
"马上搜索文件名")
if __name__ ==
'__main__':
window =
Window()
window.MainLoop()
#为单行注释
首先插入所需模块用import
tkinter GUI前段模块
threading 多线程模块
os 系统文件操作模块
创建class类
def定义函数
if __name__ =="__main__":运行函数
try:--except:为调试自动抛出异常
转载于:https://www.cnblogs.com/chauncy/p/5564345.html
相关资源:Python模块文件结构代码详解