为什么类中定义的函数在其他代码片段中不可见?

2024-10-06 11:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在玩python,在这方面我是全新的。我用tkinter和beautifulsoup之类的东西写了我的第一段“严肃”代码,它成功了。现在,为了扩展我的知识,我正在使用pyqt5重新编写它,并尝试使用类而不是“意大利面”代码。 我的程序在一般情况下工作,它阅读网站,用BeautifulSoup解析html代码,获取所需的行,等等。我使用了一些“计算器”教程作为基础,经过多次尝试和错误,我使它工作了。代码如下:

from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton, QFileDialog, QMainWindow
from PyQt5.QtCore import Qt, QDate, QTimer, QEventLoop
from PyQt5.QtGui import QIcon, QPixmap
import sys
import os
import requests
from bs4 import BeautifulSoup
from operator import itemgetter

ERROR_MSG = 'ERROR'

class BinColUI(QMainWindow):
    def createLabelTop(self):
        self.label_top = QLabel('PLEASE WAIT')
        self.label_top.setAlignment(Qt.AlignCenter)
        self.label_top.setStyleSheet("font: 14pt Bahnschrift; color: yellow")
        self.generalLayout.addWidget(self.label_top, alignment=Qt.AlignCenter)

    def createLabelBot(self):
        self.label_bot = QLabel('PLEASE WAIT')
        self.label_bot.setAlignment(Qt.AlignCenter)
        self.label_bot.setStyleSheet("font: 14pt Bahnschrift; color: yellow")
        self.generalLayout.addWidget(self.label_bot, alignment=Qt.AlignCenter)

    def setLabels(self, texttop, textbot):
        self.label_top.setText(texttop)
        self.label_bot.setText(textbot)

    def createLabelImg(self):
        label_img = QLabel()
        label_img.setFixedSize(self.window().width(), 300)
        label_img.setAlignment(Qt.AlignCenter)
        image = 'img\pleasewait'
        pixmap = QPixmap(resource_path(image+'.png'))
        pixmap = pixmap.scaledToHeight(label_img.height(), Qt.SmoothTransformation)
        label_img.setPixmap(pixmap)
        self.generalLayout.addWidget(label_img, alignment=Qt.AlignCenter)

    def setLabelImg(self, bin_color):
        image = 'img\'+bin_color'
        pixmap = QPixmap(resource_path(image + '.png'))
        pixmap = pixmap.scaledToHeight(self.label_img.height(), Qt.SmoothTransformation)
        self.label_img.setPixmap(pixmap)

    def __init__(self):
        super().__init__()
        self.setWindowTitle('Bin Collection')
        self.setFixedSize(500, 500)
        self.setStyleSheet('background-color: #7C7D7B')
        self.generalLayout = QVBoxLayout()
        self._centralWidget = QWidget(self)
        self.setCentralWidget(self._centralWidget)
        self._centralWidget.setLayout(self.generalLayout)
        self.createLabelTop()
        self.createLabelImg()
        self.createLabelBot()

class BinColCtrl:
    def __init__(self, model, view):
        self._evaluate = model
        self._view = view
        self.calculateResult()

    def calculateResult(self):
        line_top = parseGoodLines(0)
        line_bottom = parseGoodLines(1)
        self._view.setLabels(line_top, line_bottom)
        self._view.
        '''

        Why the function setLabelImg from class BinColUi is not visible here?
        I can call setLabel (as shown above) but no setLabelImg.

        '''
def parseGoodLines(linia_number):
    global bin_color
    try:
        if linia_number==0:
            start_text='Your next collection is '
        else:
            start_text='After that: '
        kosz_name = good_lines[linia_number][0]
        kosz_date = good_lines[linia_number][1]
        kosz_date_str = QDate.toString(kosz_date, 'dd MMMM yyyy')
        ile_dni=QDate.currentDate().daysTo(kosz_date)
        result = '%s%s\nYou need to put it outside before %s\nIt\'s in %s days' \
                 %(start_text, str.upper(kosz_name), kosz_date_str, str(ile_dni))
    except Exception:
        result = ERROR_MSG
    return result

def resource_path(relative_path):
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

class MakeSoup:
    def getDataFromWebsite(self):
        URL = 'http://mydurham.durham.gov.uk/article/12690?uprn=100110375827'
        page = requests.get(URL)
        soup = BeautifulSoup(page.content, 'html.parser')
        results = soup.find(id='page_PageContentHolder_template_pnlArticleBody')
        return results

    def mixSoup(self, dane):
        linie_ze_strony = dane.find_all('p')
        global good_lines
        good_lines=[]
        for kosz in linie_ze_strony:
            linia_bez_p = str(kosz).replace('<p>', "")
            linia_bez_p = str(linia_bez_p).replace('</p>', "")
            kosz = linia_bez_p
            if 'Your next ' in str(kosz):
                if 'rubbish' in str(kosz):
                    rubbish_len = len(str(kosz)) - 1
                    date_rubbish = str(kosz)[33:rubbish_len]
                if 'recycling' in str(kosz):
                    recycle_len = len(str(kosz)) - 1
                    date_recycle = str(kosz)[35:recycle_len]
        qdate_rubbish = QDate.fromString(date_rubbish, 'dd MMMM yyyy')
        qdate_recycle = QDate.fromString(date_recycle, 'dd MMMM yyyy')
        good_lines.append(['Rubbish', qdate_rubbish])
        good_lines.append(['Recycling', qdate_recycle])
        good_lines.sort(key=itemgetter(1))
        return good_lines

    def __init__(self):
        self.mixSoup(self.getDataFromWebsite())

def main():
    bincol = QApplication(sys.argv)
    view = BinColUI()
    view.show()
    MakeSoup()
    model = parseGoodLines
    BinColCtrl(model=model, view=view)
    sys.exit(bincol.exec_())

if __name__ == '__main__':
    main()

在BinColUi类中,我有一些函数,稍后我将使用这些函数来构建和更改一些视觉元素。在BinColCtrl类中,我使用函数setLabels从BinColUi类更改标签上的文本,它工作正常。但是,当我试图从同一个类调用函数setLabelImg时,它是不可见的(参见附件中的图片),我不知道为什么。实际上,只有函数setLabels可用

enter image description here


Tags: pathimportselfviewimgdatedefqt
2条回答

您的IDE似乎不知道self._view对象的类型。它看到setLabels,因为它在上面的行中使用

尝试添加类型批注,如下所示:

class BinColCtrl:
    def __init__(self, model, view):
       self._evaluate = model
       self._view: BinColUi = view  # <  this type annotation might help
       self.calculateResult()

您可以找到有关类型批注here的更多信息

您的IDE不知道self._view的类型。它只知道self._view有一个setLabels属性,因为您刚刚使用了它

用正确的类型注释变量,IDE就可以发现该方法

class BinColCtrl:
    # ``: BinColUI`` tells the IDE the type of ``view`` and ``self._view``
    def __init__(self, model, view: BinColUI):
        self._evaluate = model
        self._view = view
        self.calculateResult()

相关问题 更多 >