-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.py
149 lines (102 loc) · 4.17 KB
/
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import traceback
# Для отлова всех исключений, которые в слотах Qt могут "затеряться" и привести к тихому падению
def log_uncaught_exceptions(ex_cls, ex, tb):
text = '{}: {}:\n'.format(ex_cls.__name__, ex)
text += ''.join(traceback.format_tb(tb))
print('Error: ', text)
QMessageBox.critical(None, 'Error', text)
quit()
import sys
sys.excepthook = log_uncaught_exceptions
from main import collect_user_comments
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class CollectCommentsThread(QThread):
about_new_text = pyqtSignal(str)
about_progress = pyqtSignal(int)
about_range_progress = pyqtSignal(int, int)
def __init__(self, user=None, url=None):
super().__init__()
self.user = user
self.url = url
self._is_run = False
def run(self):
print('Start thread.')
try:
collect_user_comments(
self.user, self.url,
handler_log_func=self.about_new_text.emit,
handler_progress_func=self.about_progress.emit,
handler_range_progress_func=self.about_range_progress.emit,
is_stop_func=lambda x=None: not self._is_run,
)
finally:
print('Finish thread.')
def start(self, priority=QThread.InheritPriority):
self._is_run = True
super().start(priority)
def exit(self, return_code=0):
self._is_run = False
return super().exit(return_code)
class MainWindow(QWidget):
# __init__ -- конструктор
def __init__(self):
# super() -- обращение к предку, т.е. в данном классе к QWidget и у него вызываем конструктор
super().__init__()
self.setWindowTitle('Mintmanga_comment_searching_by_nickname')
layout = QVBoxLayout()
self.nick_line_edit = QLineEdit()
self.nick_line_edit.setText('gil9red')
self.url_line_edit = QLineEdit()
self.url_line_edit.setText('https://mintmanga.live/tokiiskii_gul/vol1/1?mtr=true')
self.start_stop_button = QPushButton()
self.start_stop_button.clicked.connect(self.start_stop)
# TODO: кликабельные ссылки на главы
self.log = QTextEdit()
self.log.setReadOnly(True)
self.progress = QProgressBar()
control_layout = QFormLayout()
control_layout.addRow('Ник:', self.nick_line_edit)
control_layout.addRow('Url манги:', self.url_line_edit)
layout.addLayout(control_layout)
layout.addWidget(self.start_stop_button)
layout.addWidget(self.log)
layout.addWidget(self.progress)
self.setLayout(layout)
self.thread = CollectCommentsThread()
self.thread.about_new_text.connect(self.log.append)
self.thread.about_progress.connect(self.progress.setValue)
self.thread.about_range_progress.connect(self.progress.setRange)
self.thread.started.connect(self._start)
self.thread.finished.connect(self._finished)
self._update_states()
def start_stop(self):
# Если поток запущен, останавливаем его, иначе -- запускаем
if self.thread.isRunning():
self.thread.exit()
else:
self.thread.user = self.nick_line_edit.text()
self.thread.url = self.url_line_edit.text()
self.thread.start()
self._update_states()
def _start(self):
self.log.clear()
self.progress.setValue(0)
def _finished(self):
self._update_states()
def _update_states(self):
self.start_stop_button.setText('Стоп' if self.thread.isRunning() else 'Начать поиск')
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
super().keyPressEvent(event)
def closeEvent(self, event):
quit()
if __name__ == '__main__':
app = QApplication([])
mw = MainWindow()
mw.resize(650, 500)
mw.show()
app.exec()