程序运行正常但QLabel无法点击。
from PyQt5.QtWidgets import *
import sys
import bs4
import urllib.request
from PyQt5.QtWidgets import *
import sys
from PyQt5.QtWidgets import QApplication
webpage=urllib.request.urlopen('http://www.boluolay.com/')
soup = bs4.BeautifulSoup(webpage, 'html.parser')
class ButonluPencere(QWidget):
def __init__(self):
super().__init__()
self.resize(640, 480)
buton = QPushButton(self)
buton.setText("Bolu Olay Gazetesi")
buton.setGeometry(400,10,100,50)
buton.clicked.connect(self.getir)
kayan=QScrollBar(self)
kayan.setGeometry(370, 60, 16, 160)
buton.clicked.connect(self.boluolay_koseyazilari)
buton.clicked.connect(self.boluolay_manset)
def getir(self):
boluolay_manset=QLabel(self)
boluolay_manset.setText("MANŞET")
boluolay_manset.setGeometry(100,-15,500,70)
boluolay_manset.show()
satirBoyu=20
i=1
for yazarlar in soup.find_all("a",class_="main_cuff"):
for yazar_adi in yazarlar.find_all("div",class_="title"):
etiket = QLabel(self)
etiket.setOpenExternalLinks(True)
etiket.setGeometry(100,satirBoyu,500,satirBoyu+20)
etiket.setText('<a href="http://www.boluolay.com'+yazarlar['href']+'">'+str(i) + ". " + yazar_adi.text.strip() + '</a>')
etiket.show()
i+=1
satirBoyu += 10
def boluolay_manset(self):
boluolay_manset=QLabel(self)
boluolay_manset.setText("DİĞER HABERLER")
boluolay_manset.setGeometry(100,180,500,70)
boluolay_manset.show()
satirBoyu=140
i=1
for manset in soup.find_all("ul",class_="news marginTen"):
for diger_haber in manset("a",class_="title"):
etiket = QLabel(self)
etiket.setOpenExternalLinks(True)
etiket.setGeometry(100,satirBoyu,500,satirBoyu+50)
etiket.setText('<a href="http://www.boluolay.com/'+manset.find('a')['href']+'">'+str(i) + ". " + diger_haber.text.strip() + '</a>')
etiket.show()
i+=1
satirBoyu += 10
def boluolay_koseyazilari(self):
boluolay_ky=QLabel(self)
boluolay_ky.setText("KÖŞE YAZARLARI")
boluolay_ky.setGeometry(100,450,500,70)
boluolay_ky.show()
webpage=urllib.request.urlopen('http://www.boluolay.com/yazarlar.html')
soup = bs4.BeautifulSoup(webpage, 'html.parser')
satirBoyu=320
i=1
for manset in soup.find_all("div",id="yazark"):
for mansetb in manset.find_all("b"):
for yazar_konu in manset("span"):
ab=yazar_konu
etiket = QLabel(self)
etiket.setOpenExternalLinks(True)
etiket.setGeometry(100,satirBoyu,500,satirBoyu+50)
etiket.setText('<a href="http://www.boluolay.com/'+manset.find('a')['href']+'">'+str(i) + ". " + ab.text.strip() + '</a>')
etiket.show()
i+=1
satirBoyu += 10
uygulama = QApplication(sys.argv)
pencere = ButonluPencere()
pencere.show()
uygulama.exec_()
答案 0 :(得分:0)
如果您想点击QLabel的网址,那么您需要为其设置一些属性(setOpenExternalLinks
除外),例如setTextInteractionFlags
到Qt::TextBrowserInteraction
和{{ 1}}到setTextFormat
编辑(将这些内容添加到您需要点击Qt::RichText
上的网址的每个标签上):
etiket
答案 1 :(得分:0)
QLabel一般不可点击,但您可以重载QLabel.mousePressEvent
或QLabel.mouseReleaseEvent
方法来识别点击信号;像这样:
class Form(QWidget):
def __init__(self):
QDialog.__init__(self)
self.t=QLabel('Click Me',self)
self.t.mousePressEvent=self.labelClick
def labelClick(self,e):
if e.button() == Qt.LeftButton:
print('Clicked')
QLabel.mousePressEvent(self.t,e)