问题
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 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_())
and 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")
# ....
# ....
# ....
and my error message
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'
回答1:
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://stackoverflow.com/questions/35182936/attributeerror-function-object-has-no-attribute-self