我一直在努力解决这个问题,将男性或女性添加到listbox1或者2就像魅力一样,当我尝试删除它确实有效但是当我选择其中一个项目时就说列表框一个我进入列表框2它仍然保留了我在列表框中激活的选择,因此当我点击删除Iam删除listbox1和listbox2中的两个突出显示的项目时,现在iam挣扎的是编辑部分,我不知道该怎么做但是让我解释一下,我想告诉它,当选择listbox1时,只编辑所选项目,无论它是在listbox1还是2中,但是对于某些阅读它只会在我尝试编辑时更改listbox2的第二行,然后它还会将它添加到listbox1这里是我目前的脚本代码:
import sys
from addlist import *
from PyQt4.QtGui import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.btnAddName, QtCore.SIGNAL('clicked()'), self.addtolist)#calls the addtolist function
QtCore.QObject.connect(self.ui.btnDelete, QtCore.SIGNAL('clicked()'), self.delete)#calls the delete function
QtCore.QObject.connect(self.ui.btnEdit, QtCore.SIGNAL('clicked()'), self.edit)#calls the edit function
self.ui.rbMale.setChecked(1) #radio button rbMale checked by default when application loads
#conditions whether name should be added to the Girls List or Boys List
def addtolist(self):
if self.ui.rbMale.isChecked()==True:
self.ui.lstBoys.addItem(self.ui.edtName.text())
self.ui.edtName.setText('')
self.ui.edtName.setFocus()
elif self.ui.rbFemale.isChecked()==True:
self.ui.lstGirls.addItem(self.ui.edtName.text())
self.ui.edtName.setText('')
self.ui.edtName.setFocus()
#conditions whether name in Girls List or Boys List should be deleted
def delete(self):
self.ui.lstBoys.currentRow()
self.ui.lstGirls.currentRow()
self.ui.lstBoys.takeItem(self.ui.lstBoys.currentRow())
self.ui.lstGirls.takeItem(self.ui.lstGirls.currentRow())
def edit(self):
#item1=self.ui.lstBoys.currentRow()
#item2=self.ui.lstGirls.currentRow()
if self.ui.lstBoys.currentRow():
newtext, ok=QInputDialog.getText(self, "Enter new Name", "Enter new Name")
if ok and (len(newtext)!=0):
self.ui.lstBoys.takeItem(self.ui.lstBoys.currentRow())
self.ui.lstBoys.insertItem(self.ui.lstBoys.currentRow(), QListWidgetItem(newtext))
else:
newtext1, oke=QInputDialog.getText(self, "Enter new Name", "Enter new Name")
if oke and (len(newtext1)!=0):
self.ui.lstGirls.takeItem(self.ui.lstGirls.currentRow())
self.ui.lstGirls.insertItem(self.ui.lstGirls.currentRow(), QListWidgetItem(newtext1))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
非常感谢协助。 谢谢
根据要求,请参阅以下内容:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'addlist.ui'
#
# Created: Mon Aug 7 09:31:01 2017
# by: PyQt4 UI code generator 4.9.6
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(678, 420)
self.btnAddName = QtGui.QPushButton(Dialog)
self.btnAddName.setGeometry(QtCore.QRect(490, 310, 93, 51))
self.btnAddName.setObjectName(_fromUtf8("btnAddName"))
self.btnDelete = QtGui.QPushButton(Dialog)
self.btnDelete.setGeometry(QtCore.QRect(290, 90, 93, 28))
self.btnDelete.setObjectName(_fromUtf8("btnDelete"))
self.btnEdit = QtGui.QPushButton(Dialog)
self.btnEdit.setGeometry(QtCore.QRect(290, 140, 93, 28))
self.btnEdit.setObjectName(_fromUtf8("btnEdit"))
self.gpbGirls = QtGui.QGroupBox(Dialog)
self.gpbGirls.setGeometry(QtCore.QRect(90, 50, 171, 221))
self.gpbGirls.setObjectName(_fromUtf8("gpbGirls"))
self.lstGirls = QtGui.QListWidget(self.gpbGirls)
self.lstGirls.setGeometry(QtCore.QRect(10, 20, 151, 191))
self.lstGirls.setObjectName(_fromUtf8("lstGirls"))
self.gpbBoys = QtGui.QGroupBox(Dialog)
self.gpbBoys.setGeometry(QtCore.QRect(410, 50, 171, 221))
self.gpbBoys.setObjectName(_fromUtf8("gpbBoys"))
self.lstBoys = QtGui.QListWidget(self.gpbBoys)
self.lstBoys.setGeometry(QtCore.QRect(10, 20, 151, 192))
self.lstBoys.setObjectName(_fromUtf8("lstBoys"))
self.gpbGender = QtGui.QGroupBox(Dialog)
self.gpbGender.setGeometry(QtCore.QRect(260, 299, 211, 61))
self.gpbGender.setObjectName(_fromUtf8("gpbGender"))
self.rbMale = QtGui.QRadioButton(self.gpbGender)
self.rbMale.setGeometry(QtCore.QRect(20, 20, 71, 20))
self.rbMale.setObjectName(_fromUtf8("rbMale"))
self.rbFemale = QtGui.QRadioButton(self.gpbGender)
self.rbFemale.setGeometry(QtCore.QRect(110, 20, 81, 20))
self.rbFemale.setObjectName(_fromUtf8("rbFemale"))
self.gpbName = QtGui.QGroupBox(Dialog)
self.gpbName.setGeometry(QtCore.QRect(90, 300, 171, 61))
self.gpbName.setObjectName(_fromUtf8("gpbName"))
self.edtName = QtGui.QLineEdit(self.gpbName)
self.edtName.setGeometry(QtCore.QRect(2, 20, 161, 41))
self.edtName.setObjectName(_fromUtf8("edtName"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.btnAddName.setText(_translate("Dialog", "Add Name", None))
self.btnDelete.setText(_translate("Dialog", "Delete", None))
self.btnEdit.setText(_translate("Dialog", "Edit", None))
self.gpbGirls.setTitle(_translate("Dialog", "Girl\'s Names", None))
self.gpbBoys.setTitle(_translate("Dialog", "Boy\'s Names", None))
self.gpbGender.setTitle(_translate("Dialog", "Gender", None))
self.rbMale.setText(_translate("Dialog", "Male", None))
self.rbFemale.setText(_translate("Dialog", "Female", None))
self.gpbName.setTitle(_translate("Dialog", "Enter name:", None))