#_*_ coding=utf8 _*_ import sys,os from Tkinter import * import ttk import tkMessageBox import signal import tkFileDialog as filedialog import hashlib import threading class MainInter: def __init__(self,master): master.title('Gen MD5 Code') menubar=Menu(master) filemenu=Menu(menubar,tearoff=0) filemenu.add_command(label="Exit",command=self.Exitss) menubar.add_cascade(label="File",menu=filemenu) aboutmenu=Menu(menubar,tearoff=0) aboutmenu.add_command(label="about",command=self.about) menubar.add_cascade(label="About",menu=aboutmenu) master.config(menu=menubar) self.frame=Frame(master) self.buPane=Frame(master) self.comPan=Frame(master) self.Progbar=Frame(master) self.frame.grid(row=0,column=0) self.buPane.grid(row=3,column=0,sticky='NW') self.comPan.grid(row=1,column=0,sticky='NW') self.Progbar.grid(row=2,column=0,sticky='NW') self.Pathlabel=Label(self.frame,text="File:",fg="blue",pady=10,padx=10) self.Pathlabel.grid(row=0,column=0) self.PathDir=Entry(self.frame,width=45) self.PathDir.grid(row=0,column=1) self.LoadFile=Button(self.buPane,text="Load File",command=self.Loadrall,padx=5) self.LoadFile.grid(row=0,column=0,sticky='SE',padx=20) self.upFile=Button(self.buPane,text="Gen Md5",command=self.genMd5,state='disable',padx=5) self.upFile.grid(row=0,column=1,padx=20) self.Md5label=Label(self.comPan,text="Md5:",fg="blue",pady=10,padx=10) self.Md5label.grid(row=0,column=0) self.MD5code=Entry(self.comPan,width=45,state='disable') self.MD5code.grid(row=0,column=1) self.prostate=Label(self.Progbar,text="Status:",fg="blue",pady=10,padx=10) self.prostate.grid(row=0,column=1) self.Progressbar=ttk.Progressbar(self.Progbar,orient=HORIZONTAL,length=300,mode='determinate',phase=5) self.Progressbar.grid(row=0,column=2) #define genMD5 function def genMd5(self): flag=self.Progressbar Mcode= self.MD5code FilePath=self.PathDir.get().strip() self.clearEntry() self.Progressbar.start() genMd5(FilePath,flag,Mcode).start() self.MD5code['state']='normal' #define information def about(self): tkMessageBox.showinfo("about","version:1.0 ") #define exit def Exitss(self): if sys.platform.startswith('win'): sys.exit() else: os.kill(os.getpid(),signal.SIGKILL) #define load file def Loadrall(self): filename=filedialog.askopenfilename() if filename: self.PathDir.delete(0,len(self.PathDir.get())) self.PathDir.insert(0,filename) self.upFile['state']='normal' self.clearEntry() self.Progressbar['value']=0 # define clear entry def clearEntry(self): if (self.MD5code.get())>0: self.MD5code.delete(0,len(self.MD5code.get())) class genMd5(threading.Thread): def __init__(self,filepath,flag,Mcode): threading.Thread.__init__(self) self.filepath=filepath self.flag=flag self.mcode=Mcode def run(self): hashtab=hashlib.md5() buf='' try: fp=open(self.filepath,'rb') while True: buf=fp.read(8*1024*1024) if len(buf)>0: hashtab.update(buf) else: break genmd5=hashtab.hexdigest() self.mcode['state']='normal' self.mcode.insert(0,genmd5) data='' except IOError, e: raise finally: fp.close() self.flag.stop() self.flag['value']=100 if __name__=="__main__": win=Tk() win.geometry('400x270+300+300') win.resizable(False,False) app=MainInter(win) win.mainloop()
转载于:https://my.oschina.net/210920/blog/748817