介绍
这篇文章主要为大家展示了怎么实现PyQt5文本输入框自动补全QLineEdit,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
自动补全会用到的一个类
主要代码
def init_lineedit(自我): #增加自动补全 self.completer=QCompleter (items_list) #设置匹配模式有三种:Qt.MatchStartsWith开头匹配(默认)Qt.MatchContains内容匹配Qt.MatchEndsWith结尾匹配 self.completer.setFilterMode (Qt.MatchContains) #设置补全模式有三种:QCompleter。QCompleter PopupCompletion(默认)。InlineCompletion QCompleter.UnfilteredPopupCompletion self.completer.setCompletionMode (QCompleter.PopupCompletion) #给lineedit设置补全器 self.lineedit.setCompleter (self.completer) def init_combobox(自我): #增加选项元素 因为我在范围(len (items_list)): self.combobox.addItem (items_list[我]) self.combobox.setCurrentIndex (1) #增加自动补全 self.completer=QCompleter (items_list) self.completer.setFilterMode (Qt.MatchContains) self.completer.setCompletionMode (QCompleter.PopupCompletion) self.combobox.setCompleter (self.completer)
完整代码:
导入系统 从PyQt5。QtWidgets进口* 从PyQt5。QtGui进口* 从PyQt5。QtCore进口* ################################################ items_list=[“C",“c++“,“Java",“Python",“JavaScript",“c#“,“Swift",“go",“Ruby",“Lua",“PHP"】 ################################################ 类部件(QWidget): def __init__(自我,* args, * * kwargs): 超级(小部件,自我)。__init__ (* args, * * kwargs) 布局=QHBoxLayout(自我) 自我。lineedit=QLineEdit(自我,minimumWidth=200) self.combobox=QComboBox(自我,minimumWidth=200) self.combobox.setEditable(真正的) layout.addWidget (QLabel (“QLineEdit",自我)) layout.addWidget (self.lineedit) 布局。QSizePolicy addItem (QSpacerItem(20、20日。扩大,QSizePolicy.Minimum)) layout.addWidget (QLabel (“QComboBox",自我)) layout.addWidget (self.combobox) #初始化组合框 self.init_lineedit () self.init_combobox () #增加选中事件 self.combobox.activated.connect (self.on_combobox_Activate) def init_lineedit(自我): #增加自动补全 self.completer=QCompleter (items_list) #设置匹配模式有三种:Qt.MatchStartsWith开头匹配(默认)Qt.MatchContains内容匹配Qt.MatchEndsWith结尾匹配 self.completer.setFilterMode (Qt.MatchContains) #设置补全模式有三种:QCompleter。QCompleter PopupCompletion(默认)。InlineCompletion QCompleter.UnfilteredPopupCompletion self.completer.setCompletionMode (QCompleter.PopupCompletion) #给lineedit设置补全器 self.lineedit.setCompleter (self.completer) def init_combobox(自我): #增加选项元素 因为我在范围(len (items_list)): self.combobox.addItem (items_list[我]) self.combobox.setCurrentIndex (1) #增加自动补全 self.completer=QCompleter (items_list) self.completer.setFilterMode (Qt.MatchContains) self.completer.setCompletionMode (QCompleter.PopupCompletion) self.combobox.setCompleter (self.completer) def alt="怎么实现PyQt5文本输入框自动补全QLineEdit ">
导入系统 从PyQt5 #。Qt进口QCompleter 从PyQt5。Qt进口QStandardItemModel 从PyQt5。QtCore进口Qt 从PyQt5。QtCore进口pyqtSlot 从PyQt5。QtCore进口pyqtSignal 从PyQt5。QtWidgets进口QFrame 从PyQt5。QtWidgets进口QLabel 从PyQt5。QtWidgets进口QWidget 从PyQt5。QtWidgets进口QLineEdit 从PyQt5。QtWidgets进口QTextEdit 从PyQt5。QtWidgets进口QCompleter 从PyQt5。QtWidgets进口QSizePolicy 从PyQt5。QtWidgets进口QMainWindow 从PyQt5。QtWidgets进口QPushButton 从PyQt5。QtWidgets进口QGridLayout 从PyQt5。QtWidgets进口QApplication 从视图导入接口 类主窗口(QMainWindow): def __init__(自我): 超级(主窗口,自我). __init__(没有) self.setWindowTitle(“对金属腐蚀性试验仪“) self.initUI () def initUI(自我): 布局=QGridLayout () layout.setSpacing (10) 自我。loginLabel=QLabel(“邮箱:“) self.loginLabel.setAlignment (Qt.AlignRight) self.loginLabel.setStyleSheet(“颜色:rgb(20、20、20255),字体大小:16 px;粗细:大胆:text") 自我。loginTxt=QLineEdit () self.loginTxt.setText (“admin") self.loginTxt.setPlaceholderText(“用户Name") self.loginTxt.setClearButtonEnabled(真正的) self.loginTxt.textChanged.connect (self.on_loginTxt_textChanged) #绑定槽函数 自我。m_model=QStandardItemModel(0, 1,自我) m_completer=QCompleter(自我。m_model自我) self.loginTxt.setCompleter (m_completer) m_completer.activated (str) .connect (self.onTxtChoosed) 自我。pwdLabel=QLabel(“密码:“) self.pwdLabel.setAlignment (Qt.AlignRight) 自我。pwdTxt=QLineEdit () self.pwdTxt.setContextMenuPolicy (Qt.NoContextMenu) #禁止复制粘贴 self.pwdTxt.setPlaceholderText (“Password") self.pwdTxt.setText (“admin") self.pwdTxt.setEchoMode (QLineEdit.Password) self.pwdTxt.setClearButtonEnabled(真正的) 自我。registeredBtn=QPushButton(“注册“) 自我。loginBtn=QPushButton(“登陆“) 自我。headLabel=QLabel(“用户登陆“) 30 self.headLabel.resize (300) self.headLabel.setAlignment (Qt.AlignCenter) null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么实现PyQt5文本输入框自动补全QLineEdit