-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathget_md5_file_gui.py
127 lines (90 loc) · 3.15 KB
/
get_md5_file_gui.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
"""
Скрипт оконной программы, которая показывает контрольную сумму файлов, кинутых на нее через drag-and-drop.
"""
import traceback
import sys
from datetime import datetime
try:
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QLabel,
QMessageBox,
QWidget,
QFormLayout,
QVBoxLayout,
QCheckBox,
)
from PyQt5.QtCore import Qt
except:
from PyQt4.QtGui import (
QApplication,
QMainWindow,
QLabel,
QMessageBox,
QWidget,
QFormLayout,
QVBoxLayout,
QCheckBox,
)
from PyQt4.QtCore import Qt
from get_md5_file import md5sum
def log_uncaught_exceptions(ex_cls, ex, tb):
text = f"{ex_cls.__name__}: {ex}:\n"
text += "".join(traceback.format_tb(tb))
ts = datetime.today().timestamp()
with open(f"error_text_{ts}", "w", encoding="utf-8") as f:
f.write(text)
QMessageBox.critical(None, "Error", text)
sys.exit(1)
sys.excepthook = log_uncaught_exceptions
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("get_md5_file_gui")
self.setAcceptDrops(True)
self.label_file_name = QLabel()
self.label_file_name.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.label_file_name.setWordWrap(True)
self.label_md5 = QLabel()
self.label_md5.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.checkbox_copy_md5_to_clipboard = QCheckBox(
"After drop copy md5 to clipboard"
)
self.checkbox_copy_md5_to_clipboard.setChecked(False)
label_file_name_label = QLabel("File name:")
label_file_name_label.setAlignment(Qt.AlignTop | Qt.AlignLeft)
layout = QFormLayout()
layout.addRow(label_file_name_label, self.label_file_name)
layout.addRow("MD5:", self.label_md5)
main_layout = QVBoxLayout()
main_layout.addWidget(QLabel("Drag and drop the file:"))
main_layout.addLayout(layout)
main_layout.addStretch()
main_layout.addWidget(self.checkbox_copy_md5_to_clipboard)
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def dragEnterEvent(self, event):
mime = event.mimeData()
if mime.hasUrls() and len(mime.urls()) == 1:
event.acceptProposedAction()
def dropEvent(self, event):
url = event.mimeData().urls()[0]
file_name = url.toLocalFile()
md5_hex = md5sum(file_name)
self.label_file_name.setText(file_name)
self.label_md5.setText(md5_hex)
# Копирование md5 в буфер обмена
if self.checkbox_copy_md5_to_clipboard.isChecked():
clipboard = QApplication.instance().clipboard()
clipboard.setText(md5_hex)
return super().dropEvent(event)
if __name__ == "__main__":
app = QApplication([])
w = MainWindow()
w.show()
app.exec()