forked from Frutto-Hub/akniga.org_book_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_gui.py
63 lines (51 loc) · 2.23 KB
/
process_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
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import QProcess
from PyQt5.QtGui import QTextCursor
class ProcessWindow(QMdiSubWindow):
def __init__(self, url, path):
super(ProcessWindow, self).__init__()
self.process = None
self.url = url
self.path = path
uic.loadUi('ui/process.ui', self)
self.manage_form()
def print_message(self, data):
stdout = bytes(data).decode("utf8")
if not "" == stdout:
self.textConsole.moveCursor(QTextCursor.End)
self.textConsole.insertPlainText(stdout)
self.textConsole.moveCursor(QTextCursor.End)
def on_stdout(self):
self.print_message(self.process.readAllStandardOutput())
def on_stderr(self):
self.print_message(self.process.readAllStandardError())
def on_finished(self):
exitcode = self.process.exitCode()
self.textConsole.append("Процесс завершен с кодом: {0}".format(exitcode))
if exitcode == 0:
self.textConsole.append("Path: {0}".format(self.path))
def manage_form(self):
geom = self.geometry()
title_height = self.style().pixelMetric(QStyle.PM_TitleBarHeight)
self.textConsole.setGeometry(2, title_height + 2, geom.width() - 4, geom.height() - title_height - 4)
self.setWindowTitle(str(self.url))
def resizeEvent(self, event):
self.manage_form()
return super(ProcessWindow, self).resizeEvent(event)
def closeEvent(self, event):
if self.process is not None:
self.process.kill()
return super(ProcessWindow, self).closeEvent(event)
def showEvent(self, show_event):
if self.process is None:
self.textConsole.append("Waiting for start...")
self.process = QProcess()
self.process.readyReadStandardOutput.connect(self.on_stdout)
self.process.readyReadStandardError.connect(self.on_stderr)
self.process.finished.connect(self.on_finished)
try:
command = ['akniga_dl.py', self.url, self.path]
self.process.start("python", command)
except Exception as error:
self.textConsole.append(f"{error}")