AttributeError: function object has no attribute self

2024/10/14 11:16:27

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.

My codes;

from gui import Ui_mainWindow
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import mainclass 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_())

and gui.py

from PyQt5 import QtCore, QtGui, QtWidgetsclass 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")#  ....#  ....#  ....

and my error message

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

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: and set the icon/grid size to something sensible (e.g. 32 x 32), otherwise the list items will be too small to see.

https://en.xdnf.cn/q/117961.html

Related Q&A

Find file with largest number in filename in each sub-directory with python?

I am trying to find the file with the largest number in the filename in each subdirectory. This is so I can acomplish opening the most recent file in each subdirectory. Each file will follow the namin…

Selenium Python - selecting from a list on the web with no stored/embedded options

Im very new to Python so forgive me if this isnt completely comprehensible. Im trying to select from a combobox in a webpage. All the examples Ive seen online are choosing from a list where the options…

How to use a method in a class from another class that inherits from yet another class python

I have 3 classes :class Scene(object):def enter(self):passclass CentralCorridor(Scene):def enter(self):passclass Map(object):def __init__(self, start_game): passAnd the class map is initiated like this…

Finding common IDs (intersection) in two dictionaries

I wrote a piece of code that is supposed to find common intersecting IDs in line[1] in two different files. On my small sample files it works OK, but on my bigger files does not. I cannot figure out wh…

Run command line containing multiple strings from python script

Hello i am trying to autogenerate a PDF, i have made a python script that generates the wanted PDF but to generate it i have to call my_cover.py -s "Atsumi" -t "GE1.5s" -co "Ja…

Identify value across multiple columns in a dataframe that contain string from a list in python

I have a dataframe with multiple columns containing phrases. What I would like to do is identify the column (per row observation) that contains a string that exists within a pre-made list of words. Wi…

ipython like interpreter for ruby

I come from python background and am learning ruby. IPython is really awesome. I am new to ruby now, and wanted to have some sort of ipython things. As of now am having tough time, going along ruby lin…

Django dynamic verification form

Im trying to create a verification form in Django that presents a user with a list of choices, only one of which is valid.For example, for a user whose favourite pizza toppings includes pineapple and r…

Any method to denote object assignment?

Ive been studying magic methods in Python, and have been wondering if theres a way to outline the specific action of:a = MyClass(*params).method()versus:MyClass(*params).method()In the sense that, perh…

Delete lines found in file with many lines

I have an Excel file (.xls) with many lines (1008), and Im looking for lines that have anything with 2010. For example, there is a line that contains 01/06/2010, so this line would be deleted, leaving …