|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from PyQt5 import QtWidgets, uic |
| 4 | +from PyQt5.QtCore import QTimer |
| 5 | + |
| 6 | + |
| 7 | +class Screenshots(QtWidgets.QMainWindow): |
| 8 | + def __init__(self): |
| 9 | + super().__init__() |
| 10 | + uic.loadUi('app.ui', self) |
| 11 | + |
| 12 | + self.screenshot_timer = None |
| 13 | + self.stop_timer = None |
| 14 | + self.button1.clicked.connect(self.start) |
| 15 | + self.button2.clicked.connect(self.stop) |
| 16 | + self.counter = 1 |
| 17 | + self.setFixedSize(self.size()) |
| 18 | + |
| 19 | + def take_screenshot(self): |
| 20 | + try: |
| 21 | + pixmap = QtWidgets.QApplication.primaryScreen().grabWindow( |
| 22 | + QtWidgets.QApplication.desktop().winId() |
| 23 | + ) |
| 24 | + save_path = self.input3.text() |
| 25 | + if not save_path: |
| 26 | + raise ValueError('Save path is empty') |
| 27 | + if not os.path.exists(save_path): |
| 28 | + os.makedirs(save_path) |
| 29 | + save_path += f'/screenshot_{self.counter}.png' |
| 30 | + pixmap.save(save_path) |
| 31 | + self.counter += 1 |
| 32 | + except Exception as e: |
| 33 | + self.handle_error('Error taking screenshot', str(e)) |
| 34 | + |
| 35 | + def start(self): |
| 36 | + try: |
| 37 | + save_path = self.input3.text() |
| 38 | + if not save_path: |
| 39 | + raise ValueError('Save path is empty') |
| 40 | + time_interval = self.input1.text() |
| 41 | + if not time_interval.isdigit() or int(time_interval) <= 0: |
| 42 | + raise ValueError('Invalid time interval') |
| 43 | + total_interval = self.input2.text() |
| 44 | + if total_interval.lower() == 'true': |
| 45 | + total_interval = -1 |
| 46 | + elif not total_interval.isdigit() or int(total_interval) <= 0: |
| 47 | + raise ValueError('Invalid total interval') |
| 48 | + else: |
| 49 | + total_interval = int(total_interval) |
| 50 | + if not os.path.exists(save_path): |
| 51 | + os.makedirs(save_path) |
| 52 | + |
| 53 | + self.input1.setEnabled(False) |
| 54 | + self.input2.setEnabled(False) |
| 55 | + self.input3.setEnabled(False) |
| 56 | + self.button1.setEnabled(False) |
| 57 | + |
| 58 | + start_message = QtWidgets.QMessageBox(self) |
| 59 | + start_message.setStyleSheet('color: white; font-size: 12px;') |
| 60 | + start_message.setIcon(QtWidgets.QMessageBox.Icon.Information) |
| 61 | + start_message.setText('Program is starting...') |
| 62 | + start_message.setWindowTitle('Starting') |
| 63 | + start_message.setStandardButtons( |
| 64 | + QtWidgets.QMessageBox.StandardButton.Ok |
| 65 | + ) |
| 66 | + start_message.show() |
| 67 | + |
| 68 | + self.screenshot_timer = QTimer() |
| 69 | + self.screenshot_timer.timeout.connect(self.take_screenshot) |
| 70 | + self.screenshot_timer.start(int(time_interval) * 1000) |
| 71 | + |
| 72 | + if total_interval != -1: |
| 73 | + self.stop_timer = QTimer() |
| 74 | + self.stop_timer.setSingleShot(True) |
| 75 | + self.stop_timer.timeout.connect(self.stop) |
| 76 | + self.stop_timer.start(total_interval * 60000) |
| 77 | + |
| 78 | + except Exception as e: |
| 79 | + error_message = QtWidgets.QMessageBox(self) |
| 80 | + error_message.setStyleSheet('color: white; font-size: 12px;') |
| 81 | + error_message.setIcon(QtWidgets.QMessageBox.Icon.Critical) |
| 82 | + error_message.setText(f'Error starting application: {str(e)}') |
| 83 | + error_message.setWindowTitle('Error') |
| 84 | + error_message.setStandardButtons( |
| 85 | + QtWidgets.QMessageBox.StandardButton.Ok |
| 86 | + ) |
| 87 | + error_message.show() |
| 88 | + |
| 89 | + def stop(self): |
| 90 | + try: |
| 91 | + if self.screenshot_timer is not None: |
| 92 | + self.screenshot_timer.stop() |
| 93 | + |
| 94 | + if self.stop_timer is not None: |
| 95 | + self.stop_timer.stop() |
| 96 | + |
| 97 | + self.input1.setEnabled(True) |
| 98 | + self.input2.setEnabled(True) |
| 99 | + self.input3.setEnabled(True) |
| 100 | + self.button1.setEnabled(True) |
| 101 | + |
| 102 | + end_message = QtWidgets.QMessageBox(self) |
| 103 | + end_message.setStyleSheet('color: white; font-size: 12px;') |
| 104 | + end_message.setIcon(QtWidgets.QMessageBox.Icon.Information) |
| 105 | + end_message.setText('Program has ended.') |
| 106 | + end_message.setWindowTitle('Ending') |
| 107 | + end_message.setStandardButtons( |
| 108 | + QtWidgets.QMessageBox.StandardButton.Ok |
| 109 | + ) |
| 110 | + end_message.show() |
| 111 | + |
| 112 | + except Exception as e: |
| 113 | + error_message = QtWidgets.QMessageBox(self) |
| 114 | + error_message.setStyleSheet('color: white; font-size: 12px;') |
| 115 | + error_message.setIcon(QtWidgets.QMessageBox.Icon.Critical) |
| 116 | + error_message.setText( |
| 117 | + 'An error occurred while stopping the application. Please try again or contact support.' |
| 118 | + ) |
| 119 | + error_message.setWindowTitle('Error') |
| 120 | + error_message.setStandardButtons( |
| 121 | + QtWidgets.QMessageBox.StandardButton.Ok |
| 122 | + ) |
| 123 | + error_message.show() |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + app = QtWidgets.QApplication(sys.argv) |
| 128 | + window = Screenshots() |
| 129 | + window.show() |
| 130 | + sys.exit(app.exec()) |
0 commit comments