md5计算
记个md5运算的,直接把文件拖到框里。关键就是那个 wx.FileDropTarget(见上1篇那个转载)。
里面有n多乱七八糟的东西,懒的删了,精简化,只有几行。
=====================================================
import wx
import sys,os
import md5
def getmd5(md5file):
return md5.new(open(md5file,'rb').read()).hexdigest()
class filedrop(wx.FileDropTarget):
def __init__(self,window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self,x,y,filenames):
for f in filenames:
self.window.AppendText('\n%s\t%s\n' %(f,getmd5(f)))
class md5frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'MD5 CHECK')
#print dir(self)
p = wx.Panel(self)
#print dir(p)
menubar = wx.MenuBar()
menu_file = wx.Menu()
menubar.Append(menu_file,'File')
menu_fileopen = menu_file.Append(-1,'Open')
menu_exit = wx.Menu()
menubar.Append(menu_exit,'Exit')
menu_exitexit = menu_exit.Append(-1,'Exit')
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU,self.openfile,menu_fileopen)
self.Bind(wx.EVT_MENU,self.Onexit,menu_exitexit)
self.txtpanel = wx.TextCtrl(p,1,style=wx.TE_MULTILINE,size=p.Size)
self.dragfiles = filedrop(self.txtpanel)
self.txtpanel.SetDropTarget(self.dragfiles)
self.filesign = 1
def openfile(self,evt):
dialog = wx.FileDialog(None,'choose a file',os.getcwd(),'','',wx.OPEN)
#print dir(dialog)
if dialog.ShowModal() == wx.ID_OK:
md5file = dialog.GetPath()
dialog.Destroy()
if self.filesign == 1:
self.md5file_1 = md5file
self.md5_file1 = getmd5(md5file)
self.txtpanel.AppendText('\n%s\t%s\n' %(self.md5file_1,self.md5_file1))
self.filesign = 2
elif self.filesign == 2:
self.md5file_2 = md5file
self.md5_file2 = getmd5(md5file)
self.txtpanel.AppendText('\n%s\t%s\n' %(self.md5file_2,self.md5_file2))
self.filesign = 1
if self.md5_file1 and self.md5_file2:
if self.md5_file1 == self.md5_file2: self.txtpanel.AppendText('%s md5 match %s md5' %(self.md5file_1,self.md5file_2))
else: self.txtpanel.AppendText('%s md5 doesnot match %s md5' %(self.md5file_1,self.md5file_2))
def Onexit(self,evt):
self.Close()
def getmd5(self,md5file):
return md5.new(open(md5file,'rb').read()).hexdigest()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = md5frame()
frame.Show()
app.MainLoop()
=====================================================