-
Notifications
You must be signed in to change notification settings - Fork 28
/
newsWindow.py
82 lines (66 loc) · 3.02 KB
/
newsWindow.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
from PyQt5.QtWidgets import QWidget,QLabel,QGridLayout,QMessageBox,QVBoxLayout,QTabWidget,QScrollArea,QFormLayout,QSizePolicy
from PyQt5.QtGui import QIcon,QImage,QPixmap
from PyQt5.QtCore import Qt
from asset.modules.newsLib.newsLibrary import news_func
import urllib.request
class newsBox(QWidget):
def __init__(self,passedKeywords):
super(newsBox,self).__init__()
self.keywords = passedKeywords
self.initUI()
def initUI(self):
self.mainLayout = QVBoxLayout()
self.lowerLayout = TabLayout(self.keywords)
self.mainLayout.addWidget(self.lowerLayout.customTab)
self.setLayout(self.mainLayout)
self.setWindowTitle('GROOT')
self.setWindowIcon(QIcon('asset/img/groot.png'))
self.setGeometry(150,150,800,500)
self.show()
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
#Function to display warning message for quitting
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message', "Are you sure to quit?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
class TabLayout(QTabWidget):
def __init__(self,passedKeywords):
super(TabLayout,self).__init__()
self.keywords = passedKeywords
self.customTab = QTabWidget()
self.news = QScrollArea()
self.scroll_widget = QWidget()
self.scroll_layout = QVBoxLayout(self.scroll_widget)
self.scroll_layout.setSpacing(20.0)
articles = news_func(self.keywords)
for article in articles:
self.myLabel = Tiles(parent="None",description = article['description'][0:100],title=article['title'],url=article['url'],urlImage=article['urlToImage'])
self.scroll_layout.addLayout(self.myLabel.tileLayout)
self.news.setWidget(self.scroll_widget)
self.customTab.addTab(self.news,"News")
class Tiles(QWidget):
def __init__(self,parent="None",title="None",description="None",url="None",urlImage="None"):
super(Tiles,self).__init__()
#Heading Widget
self.heading = QLabel('<b>%s</b>'%title)
#SubHeading Widget with link to open in browser
self.subHeading = QLabel('{}<a href="{}">...more</a>'.format(description,url))
self.subHeading.setOpenExternalLinks(True)
#Image Widget with article
try:
data = urllib.request.urlopen(urlImage).read()
except:
data = urllib.request.urlopen("https://ibb.co/XY30sjs").read()
self.image = QImage()
self.image.loadFromData(data)
self.imageLabel = QLabel("image")
self.imageLabel.setPixmap(QPixmap(self.image).scaled(64,64,Qt.KeepAspectRatio))
self.tileLayout = QGridLayout()
self.tileLayout.addWidget(self.heading,1,0)
self.tileLayout.addWidget(self.imageLabel,1,1)
self.tileLayout.addWidget(self.subHeading,2,0)