-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (78 loc) · 3.22 KB
/
main.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
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QProgressBar, QVBoxLayout, \
QWidget, QHeaderView, QPushButton
from youtubeDownloadThread import Thread
url_arr = [
'https://www.youtube.com/watch?v=EuS_bvQj--U&list=LL&index=9&pp=gAQBiAQB',
'https://www.youtube.com/watch?v=gyjE1qIPohA&list=LL&index=10&pp=gAQBiAQB',
'https://www.youtube.com/watch?v=fC9jISKU25E&list=LL&index=11&pp=gAQBiAQB',
'https://www.youtube.com/watch?v=jUs-ITmi_u4&list=LL&index=12&pp=gAQBiAQB',
'https://www.youtube.com/watch?v=Qw88zQTxq48&list=LL&index=13&pp=gAQBiAQB',
]
class ProgressBarTableWidget(QTableWidget):
def __init__(self):
super().__init__()
self.__initVal()
self.__initUi()
def __initVal(self):
self.__items = []
self.__threads = []
def __initUi(self):
cols = ['Name', 'Progress']
# Add some header labels
self.setColumnCount(len(cols))
self.setHorizontalHeaderLabels(cols)
self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
def setProgressItems(self, items):
self.__items = items
for r_idx in range(len(self.__items)):
self.setItem(r_idx, 0, QTableWidgetItem(self.__items[r_idx]))
progressBar = QProgressBar(self)
self.setCellWidget(r_idx, 1, progressBar)
self.setItem(r_idx, 2, QTableWidgetItem('In Progress'))
self.resizeColumnsToContents()
def runItemFromRow(self, r_idx):
item_text = self.item(r_idx, 0).text()
t = Thread(item_text)
self.__threads.append(t)
t.started.connect(self.__started)
t.finished.connect(self.__finished)
# This ensures that the thread is removed from the list once it finishes
t.finished.connect(lambda: self.__cleanupThread(t))
progressbar = self.cellWidget(r_idx, 1)
# Set maximum to file size for accurate progress bar scaling
t.updateMaximumSize.connect(progressbar.setMaximum)
t.progressUpdate.connect(progressbar.setValue)
t.start()
def __started(self):
print('started')
def __finished(self):
print('finished')
def __cleanupThread(self, thread):
thread.deleteLater() # Schedule the thread object for deletion
self.__threads.remove(thread) # Remove thread from the list
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
self.setWindowTitle('ProgressBar TableWidget!')
self.__runBtn = QPushButton('Run!')
self.__runBtn.clicked.connect(self.__run)
self.__tableWidget = ProgressBarTableWidget()
self.__tableWidget.setRowCount(len(url_arr))
self.__tableWidget.setProgressItems(url_arr)
lay = QVBoxLayout()
lay.addWidget(self.__runBtn)
lay.addWidget(self.__tableWidget)
mainWidget = QWidget()
mainWidget.setLayout(lay)
self.setCentralWidget(mainWidget)
def __run(self):
for r_idx in range(len(url_arr)):
self.__tableWidget.runItemFromRow(r_idx)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())