在这种情况下,您不需要QCompleter。请尝试以下示例:
from PyQt5 import QtCore, QtGui, QtWidgetsclass Window(QtWidgets.QMainWindow):
def __init__(self, cList):
super().__init__()
self.cList = cList
self.lineEdit_1 = QtWidgets.QLineEdit()
self.lineEdit_2 = QtWidgets.QLineEdit()
layoutH = QtWidgets.QHBoxLayout()
layoutH.addWidget(self.lineEdit_1)
layoutH.addWidget(self.lineEdit_2)#completer = QtWidgets.QCompleter(self)#model = QtCore.QStringListModel()#completer.setModel(model)
self.comboBox_4 = QtWidgets.QComboBox()#self.comboBox_4.setCompleter(completer)
self.comboBox_4.addItems(sorted(cList.keys()))
self.comboBox_4.activated[str].connect(self.onActivatedText)
layoutV = QtWidgets.QVBoxLayout()
layoutV.addLayout(layoutH)
layoutV.addWidget(self.comboBox_4)
centralWidget = QtWidgets.QWidget()
centralWidget.setLayout(layoutV)
self.setCentralWidget(centralWidget)
@QtCore.pyqtSlot(str)
def onActivatedText(self, text):
self.lineEdit_1.setText(self.cList[text][0])
self.lineEdit_2.setText(self.cList[text][1])autocompleteList2 = {
'James-1': ['James111@Gmail.com', '410-111-1111'],
'James-2': ['James222@Gmail.com', '410-222-2222'],
'James-3': ['James333@Gmail.com', '410-333-3333'],}if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window(autocompleteList2)
w.show()
sys.exit(app.exec_())
这个用python写?你要用到界面库tkinter或pygtk,pyqt下面这个代码用的是tkinter
from
tkinter
import
*
master
=
Tk()
variable
=
StringVar(master)
variable.set("中文")
#
default
value
w
=
OptionMenu(master,
variable,
"中文",
"英文",
"日文")
w.pack()
mainloop()
方法 描述
addItem() 添加一个下拉选项
addItems() 从列表中添加下拉选项
Clear() 删除下拉选项集合中的所有选项
count() 返回下拉选项集合中的数目
currentText() 返回选中选项的文本
itemText(i) 获取索引为i的item的选项文本
currentIndex() 返回选中项的索引
setItemText(int index,text) 改变序列号为index的文本
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)