AttributeError:“函数"对象没有属性“自我" [英] AttributeError: 'function' object has no attribute 'self'

查看:202
本文介绍了AttributeError:“函数"对象没有属性“自我"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gui文件,我用qtdesigner设计了它,并且还有另一个py文件.我试图更改按钮名称或试图在listwidget中添加项目,但我没有做这些事情.我收到一条错误消息.

I have a gui file and I designed it with qtdesigner, and there are another py file. I tried to changing button name or tried to add item in listwidget but I didn't make that things. I got an error message.

我的代码;

from gui import Ui_mainWindow
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import main


class Music(QtWidgets.QMainWindow, Ui_mainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.search_button.clicked.connect(self.searchbutton)

    def searchbutton(self):
        base = main.Main()
        self.url = base.search(self.search_box.text())
        self.dict = base.get_data(self.url)
        print(self.dict)
        for i in self.dict:
        self.setupUi.self.listWidget.addItem(i)


app = QtWidgets.QApplication(sys.argv)
gui = Music()
gui.show()
sys.exit(app.exec_())

和gui.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_mainWindow(object):
    def setupUi(self, mainWindow):
        mainWindow.setObjectName("mainWindow")
        mainWindow.resize(413, 613)
        mainWindow.setMinimumSize(QtCore.QSize(413, 613))
        mainWindow.setMaximumSize(QtCore.QSize(413, 613))
        mainWindow.setAutoFillBackground(False)
        self.centralwidget = QtWidgets.QWidget(mainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.musics_frame = QtWidgets.QFrame(self.centralwidget)
        self.musics_frame.setGeometry(QtCore.QRect(-1, 49, 411, 441))
        self.musics_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.musics_frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.listWidget = QtWidgets.QListWidget(self.musics_frame)
        self.listWidget.setGeometry(QtCore.QRect(10, 10, 391, 421))
        self.listWidget.setIconSize(QtCore.QSize(5, 5))
        self.listWidget.setGridSize(QtCore.QSize(5, 5))
        self.listWidget.setViewMode(QtWidgets.QListView.IconMode)
        self.listWidget.setObjectName("listWidget")
        #  ....
        #  ....
        #  ....

和我的错误消息

  File "/home/yavuz/Genel/youtube-sounds/music.py", line 19, in searchbutton
    self.setupUi.self.listWidget.addItem(i)
    AttributeError: 'function' object has no attribute 'self'

推荐答案

当您将ui类用作mixin时,来自Qt Designer的所有小部件都将添加为Music类的实例属性.所以你只想要:

When you use the ui class as a mixin, all the widgets from Qt Designer will be added as instance attributes of the Music class. So you just want:

    self.listWidget.addItem(i)

PS:并将图标/网格大小设置为合理的值(例如32 x 32),否则列表项将太小而看不到.

PS: and set the icon/grid size to something sensible (e.g. 32 x 32), otherwise the list items will be too small to see.

这篇关于AttributeError:“函数"对象没有属性“自我"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆