我有一个gui文件,我用qtdesigner设计它,还有另一个py文件。我尝试更改按钮名称或尝试在listwidget中添加项目,但我没有做到这一点。我收到了一条错误消息。
我的代码;
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'
答案 0 :(得分:0)
当您将ui类用作mixin时,Qt Designer中的所有小部件都将作为Music
类的实例属性添加。所以你只想要:
self.listWidget.addItem(i)
PS:并将图标/网格大小设置为合理的(例如32 x 32
),否则列表项将太小而无法看到。