Oo笑容太甜oO/

PYQT5-实现MD5计算器


#!/usr/bin/env python3
import sys  
import hashlib
import os
import datetime
from PyQt5.QtWidgets import *


class Example(QMainWindow): 
  def __init__(self): 
    super().__init__() 
    self.initUI()

  def initUI(self): 
    self.lbl = QLabel(self) 
    self.qle1 = QLineEdit(self)  
    self.qle1.move(60, 30) 
    self.qle2= QLineEdit(self) 
    self.qle2.move(60, 80) 

    self.lbl.move(60, 10) 
    self.qle1.setFixedWidth(300)   
    self.qle2.setFixedWidth(300) 

    self.qle1.textChanged[str].connect(self.onChanged) 

    self.btn1 = QPushButton("计算MD5", self)  
    self.btn1.move(60, 200)  
    self.btn2 = QPushButton("打开文件", self)  
    self.btn2.move(250, 200) 

    self.btn1.clicked.connect(self.button1Clicked) 
    self.btn2.clicked.connect(self.button2Clicked) 

    self.statusBar()  
    self.setGeometry(300, 300, 420, 300)  
    self.setWindowTitle('MD5') 
    self.show()


  def buttonClicked(self): 
    sender = self.sender()
    self.statusBar().showMessage(sender.text() + ' was pressed')  

  def onChanged(self, text): 

    self.lbl.setText(text) 
    self.lbl.adjustSize()

  

  #def onChanged(self, text):
  #    src=text
  #    m = hashlib.md5()
  #    m.update(src.encode('UTF-8'))
  #    self.lbl.setText(m.hexdigest())
  #    self.lbl.adjustSize()  


  def button1Clicked(self):
    #计算字符串MD5
    src=self.qle1.text()
    m = hashlib.md5()
    m.update(src.encode('UTF-8'))
    self.qle2.setText(m.hexdigest())
  
  def button2Clicked(self):
    
    filename, filetype=QFileDialog.getOpenFileName(self,'open file','.', 'All Files (*)')
    if not os.path.isfile(filename):
      return
        #开始计时
    starttime = datetime.datetime.now()
    #计算文件MD5
    myhash = hashlib.md5()
    f = open(filename,'rb')
    while True:
      b = f.read(8096)
      if not b :
        break
      myhash.update(b)
    f.close()
    self.qle2.setText(myhash.hexdigest())
    #self.qle2.setText(filename)
        #结束计时
    endtime = datetime.datetime.now()
    self.statusBar().showMessage('计算%s文件MD5值运行时间:%ds'%(os.path.basename(filename),(endtime-starttime).seconds)) 






if __name__ == '__main__':  
          
    app = QApplication(sys.argv)  
    ex = Example()  
    sys.exit(app.exec_())