我发现一些代码符合我的目的,而且我已经将它增加了一点我
可以在其上添加项目,但我无法将其从屏幕上删除。
它是在选择时删除相对线的函数:
self.listWidget.removeItemWidget(self.list[self.list.indexAt(self.lineEdit.text())])
如果你想运行它,这是我的整个代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(535, 302)
self.listWidget = QtWidgets.QListWidget(Form)
self.listWidget.setGeometry(QtCore.QRect(50, 60, 256, 192))
self.listWidget.setObjectName("listWidget")
self.list = self.listWidget
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(320, 130, 90, 33))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(420, 130, 90, 33))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(329, 190, 181, 61))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(50, 10, 431, 31))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(310, 70, 221, 33))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "ekle"))
self.pushButton.clicked.connect(self.list_add)
self.pushButton_2.setText(_translate("Form", "sil"))
self.pushButton_3.setText(_translate("Form", "kapat"))
self.pushButton_3.clicked.connect(quit)
self.pushButton_2.clicked.connect(self.f1)
self.label.setText(_translate("Form", "veriler ekleniyor"))
def list_add(self):
self.label.setText(self.lineEdit.text() + " eklendi")
self.listWidget.addItem(self.lineEdit.text())
def f1(self):
self.label.setText(self.lineEdit.text() + " silindi")
self.listWidget.removeItemWidget(self.list[self.list.indexAt(self.lineEdit.text())])
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())