Skip to content

Commit 7c34b9f

Browse files
author
dookgulliver
committed
add scripts of Desbravando o módulo QtCore post
1 parent 6d07e38 commit 7c34b9f

14 files changed

+316
-0
lines changed

Post-2(QtCore)/Mp3Downloader.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from PyQt5.QtWidgets import (QWidget, QLineEdit, QPushButton, QSystemTrayIcon
2+
QFileDialog, QFormLayout, QApplication)
3+
from PyQt5.QtCore import QThread, QRegExp
4+
import requests
5+
import sys
6+
7+
8+
class DownloaderMusic(QThread):
9+
def __init__(self,name,url,path):
10+
super(DownloaderMusic,self).__init__()
11+
self.path = path
12+
self.url = url
13+
self.name = name
14+
15+
def run(self):
16+
mescl = self.path+"/"+self.name
17+
with open(mescl+'.mp3', 'wb') as f:
18+
f.write(requests.get(self.url).content)
19+
20+
21+
class Main(QWidget):
22+
def __init__(self, parent=None):
23+
super(Main, self).__init__()
24+
self.path = None
25+
self.settings()
26+
self.create_widgets()
27+
self.create_layout()
28+
29+
def settings(self):
30+
self.resize(300,120)
31+
self.setWindowTitle("Mp3 Downloader")
32+
33+
def create_widgets(self):
34+
self.edit_url = QLineEdit()
35+
self.edit_name = QLineEdit()
36+
self.btn_select_path = QPushButton("Select path",self)
37+
self.btn_select_path.clicked.connect(self.select_path)
38+
self.btn_down = QPushButton("Download mp3",self)
39+
self.btn_down.clicked.connect(self.download)
40+
41+
def create_layout(self):
42+
self.layout = QFormLayout()
43+
self.layout.addRow("Nome:",self.edit_name)
44+
self.layout.addRow("Url:",self.edit_url)
45+
self.layout.addRow("Selecionar destino:",self.btn_select_path)
46+
self.layout.addRow(self.btn_down)
47+
self.setLayout(self.layout)
48+
49+
def select_path(self):
50+
self.path = QFileDialog.getExistingDirectory(self,"Selecionar Pasta")
51+
52+
def download(self):
53+
if self.verify_fields():
54+
self.manage_interface(False)
55+
self.thread_qt()
56+
57+
def thread_qt(self):
58+
url = self.edit_url.text()
59+
name = self.edit_name.text()
60+
path = self.path
61+
self.thre = DownloaderMusic(name, url, path)
62+
self.thre.finished.connect(self.downfin)
63+
self.thre.start()
64+
65+
def manage_interface(self,state):
66+
self.btn_down.setEnabled(state)
67+
self.edit_name.setEnabled(state)
68+
self.edit_url.setEnabled(state)
69+
self.btn_select_path.setEnabled(state)
70+
71+
def verify_fields(self):
72+
if self.path == None:
73+
return False
74+
else:
75+
strings = [self.edit_url.text(),self.edit_name.text(), self.path]
76+
regex_validate = QRegExp("*.mp3")
77+
regex_validate.setPatternSyntax(QRegExp.Wildcard)
78+
emptys = 0
79+
for string in strings:
80+
if len(string.split()) == 0:
81+
emptys+=1
82+
if emptys == 0 and regex_validate.exactMatch(self.editUrl.text()):
83+
return True
84+
85+
def downfin(self):
86+
self.notifyIcon = QSystemTrayIcon()
87+
self.notifyIcon.setVisible(True)
88+
self.notifyIcon.showMessage(
89+
"Download Finalizado",
90+
u"O download da sua música foi realizado com sucesso.",
91+
QSystemTrayIcon.Information,3000)
92+
self.manageInterface(True)
93+
94+
root = QApplication([])
95+
app = Main()
96+
app.show()
97+
sys.exit(root.exec_())

Post-2(QtCore)/imgs/Thumbs.db

83 KB
Binary file not shown.

Post-2(QtCore)/imgs/default-user.png

44.2 KB
Loading

Post-2(QtCore)/imgs/release-1.png

4.51 KB
Loading

Post-2(QtCore)/imgs/release-2.png

14.2 KB
Loading

Post-2(QtCore)/imgs/release-3.png

3.2 KB
Loading

Post-2(QtCore)/imgs/release-4.png

3.47 KB
Loading

Post-2(QtCore)/imgs/release-5.png

29 KB
Loading

Post-2(QtCore)/imgs/release-6.png

30.2 KB
Loading

Post-2(QtCore)/progress_bar.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from PyQt5.QtWidgets import QWidget, QProgressBar, QApplication
2+
from PyQt5.QtCore import QBasicTimer
3+
import sys
4+
5+
6+
class Window(QWidget):
7+
def __init__(self, parent=None):
8+
super(Window, self).__init__()
9+
self.set_settings()
10+
self.create_widgets()
11+
12+
def set_settings(self):
13+
self.resize(350,200)
14+
15+
def create_widgets(self):
16+
self.progress_bar = QProgressBar(self)
17+
self.progress_bar.setFixedWidth(300)
18+
self.progress_bar.move(50,80)
19+
#timer creating
20+
self.timer = QBasicTimer()
21+
self.step = 0
22+
self.timer.start(100, self)
23+
24+
def timerEvent(self, e):
25+
if self.step >= 100:
26+
self.timer.stop()
27+
28+
self.step += 1
29+
self.progress_bar.setValue(self.step)
30+
31+
root = QApplication(sys.argv)
32+
app = Window()
33+
app.show()
34+
sys.exit(root.exec_())

0 commit comments

Comments
 (0)