【问题标题】:QLabel is not clickable in my programQLabel 在我的程序中不可点击
【发布时间】:2016-04-24 23:12:41
【问题描述】:

程序运行正常,但 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_()

【问题讨论】:

    标签: qt python-3.x pyqt


    【解决方案1】:

    如果您想点击您的 QLabel 的 URL,那么您需要为它设置更多属性(除了 setOpenExternalLinks),例如 setTextInteractionFlagsQt::TextBrowserInteractionsetTextFormatQt::RichText

    编辑(例如,将这些行添加到您需要单击etiket 上的 URL 的每个标签中):

    etiket.setOpenExternalLinks(True)
    etiket.setTextInteractionFlags(Qt.TextBrowserInteraction)
    etiket.setTextFormat(Qt.RichText)
    

    【讨论】:

    • @BithynianPrince 我已经编辑了帖子,你至少可以自己过去吗?
    【解决方案2】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 2018-09-15
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多