python+pyqt5编写md5生成器

本文实例为大家分享了python+pyqt5编写md5生成器的具体代码,供大家参考,具体内容如下

学了一下pyqt5,写一个小程序来实践一下。

下图是程序界面:

python+pyqt5编写md5生成器_第1张图片

下面是代码:

import hashlib, os, sys

from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import (QFileDialog,
 QGridLayout, QLineEdit, QTextEdit,QLabel, QWidget, QToolTip, 
 QPushButton, QApplication)
from PyQt5.QtCore import QCoreApplication

#小文件的处理函数
def get_md5_small(file_path):
 md5 = None
 if os.path.exists(file_path) and os.path.isfile(file_path):
  with open( file_path, 'rb' ) as f:
   md5_obj = hashlib.md5()
   md5_obj.update( f.read() )
   hash_code = md5_obj.hexdigest()

   md5 = str(hash_code).lower()

 return md5

#大文件的处理函数
def get_md5_big(file_path):
 md5 = None
 if os.path.exists(file_path) and os.path.isfile(file_path):
  with open( file_path, 'rb' ) as f:
   md5_obj = hashlib.md5()
   while True:
    d = f.read(8096)
    if not d:
     break
    md5_obj.update(d)
   hash_code = md5_obj.hexdigest()
   md5 = str( hash_code ).lower()

 return md5

class MainBody(QWidget):
 def __init__(self):
  super(MainBody, self).__init__()
  self.init()

 def init(self):
  #布局
  grid = QGridLayout()
  self.setLayout(grid)
  grid.setSpacing(10)

  #文件路径
  self.m_tedit = QTextEdit()
  self.m_tedit.setToolTip( u'可以拖拽文件到这里来' )
  grid.addWidget(self.m_tedit, 1, 0, 2, 3)

  #四个按钮
  self.m_select_btn = QPushButton(u'选择文件')
  self.m_select_btn.clicked.connect( self.select_func )

  self.m_generate_btn = QPushButton(u'生成')
  self.m_generate_btn.clicked.connect( self.generate_func )

  self.m_clear_btn = QPushButton(u'清空')
  self.m_clear_btn.clicked.connect( self.clear_func )

  self.m_copy_btn = QPushButton(u'复制')
  self.m_copy_btn.clicked.connect( self.copy_func )

  grid.addWidget(self.m_select_btn, 3, 0)
  grid.addWidget(self.m_generate_btn, 3, 1)
  grid.addWidget(self.m_clear_btn, 3, 2)
  grid.addWidget(self.m_copy_btn, 5, 1)

  #md5的输出区域
  self.m_output = QLineEdit()
  grid.addWidget(self.m_output, 4, 0, 1, 3)

  self.setGeometry(300,300,300,200)
  self.setWindowTitle(u"md5生成器")
  self.setWindowIcon(QIcon('icon.png'))
  self.show()

 #选择文件
 def select_func(self):
  filename, ok = QFileDialog.getOpenFileName(self, "Open file", "C:/Users/Administrator/Desktop")
  print(os.path.getsize(filename))
  self.m_tedit.setText( filename )
  self.m_output.clear()

 #生成md5按钮
 def generate_func(self):
  path = self.m_tedit.toPlainText()
  if path.startswith( 'file:///' ):
   path = path[8:]

  if os.path.exists(path) and os.path.isfile(path):
   #200M以上的当作大文件
   if os.path.getsize(path) >= 200*1024*1204:
    md5 = get_md5_big( path )
   else:
    md5 = get_md5_small( path )
   self.m_output.setText( md5 )

 def clear_func(self):
  self.m_tedit.clear()
  self.m_output.clear()

 #复制
 def copy_func(self):
  clipboard = QApplication.clipboard()
  clipboard.setText( self.m_output.text() )

if __name__ == "__main__":
 app = QApplication(sys.argv)
 mb = MainBody()
 sys.exit( app.exec_() )

最后用 pyinstaller 打包成 exe 文件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(python+pyqt5编写md5生成器)