forked from hyzyla/InstabotGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainWindow.py
executable file
·118 lines (93 loc) · 3.52 KB
/
mainWindow.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
import atexit
import multiprocessing
import pickle
import signal
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGridLayout, QWidget, QStyle,
QPushButton, QFrame)
from account import Account
from accountTable import AccountTable
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.accountTable = AccountTable()
self.addNewAccountButton = QPushButton("New")
self.removeAccountButton = QPushButton("Remove")
self.startAllButton = QPushButton("Start all")
self.configUI()
self.bindWidgets()
self.restoreSession()
def restoreSession(self):
# Do nothing when file not found
try:
with open('data.pickle', 'rb') as f:
rows = pickle.load(f)
except FileNotFoundError:
return
# Adding rows to table
for credentials, stats in zip(*rows):
account = Account(credentials=credentials)
account.setStats(stats)
self.accountTable.addNewRow(account)
def addNewAccount(self):
account = Account()
dlg = account.settingDialog
if not dlg.exec_():
return
self.accountTable.addNewRow(account)
account.startWorking()
def bindWidgets(self):
self.addNewAccountButton.clicked.connect(self.addNewAccount)
self.removeAccountButton.clicked.connect(self.removeAccount)
self.startAllButton.clicked.connect(self.startAll)
def startAll(self):
self.accountTable.startAll()
def stopAll(self):
self.accountTable.stopAll()
def removeAccount(self):
self.accountTable.removeCurrent()
def configUI(self):
layout = QGridLayout()
widget = QWidget()
widget.setLayout(layout)
controlFrame = self.configControlFrame()
layout.addWidget(controlFrame, 1, 0)
layout.addWidget(self.accountTable, 2, 0)
self.resize(800, 600)
self.setCentralWidget(widget)
self.setWindowTitle("InstaBot")
icon = self.style().standardIcon(QStyle.SP_FileDialogListView)
self.setWindowIcon(icon)
self.show()
def configControlFrame(self):
layout = QGridLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setColumnStretch(2, 1)
layout.addWidget(self.addNewAccountButton, 0, 0, Qt.AlignLeft)
layout.addWidget(self.removeAccountButton, 0, 1, Qt.AlignLeft)
layout.addWidget(self.startAllButton, 0, 3, Qt.AlignRight)
frame = QFrame()
frame.setLayout(layout)
return frame
def exit_handler(window):
rows = window.accountTable.rows
for row in rows:
row.stopWorking()
credentialsRows = [r.getSettings() for r in rows]
statsRow = [r.getStats() for r in rows]
with open('data.pickle', 'wb') as f:
pickle.dump([credentialsRows, statsRow], f, pickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
multiprocessing.freeze_support()
app = QApplication(sys.argv)
with open("style.qss") as fileStyle:
style = fileStyle.read()
app.setStyleSheet(style)
window = MainWindow()
atexit.register(lambda: exit_handler(window))
signal.signal(signal.SIGTERM, lambda: exit_handler(window))
try:
sys.exit(app.exec_())
except:
exit_handler(window)