本文实例为大家分享了python+pyqt5编写md5生成器的具体代码,供大家参考,具体内容如下
学了一下pyqt5,写一个小程序来实践一下。
下图是程序界面:
下面是代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
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 文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u012755940/article/details/78273819