|
参考:https://zhuanlan.zhihu.com/p/138386185https://developer.aliyun.com/article/1314474一、组合框控件ComboBoxComboBox的功能是从一个列表中一次只能选取或输入一个选项,其主要特点是具有带向下箭头的方框。在程序执行时,按下此按钮就会下拉出一个列表框供用户选择项目属性说明Items表示该组合框中所包含项的集合SelectedItem获取或设置当前组合框中选定项的索引SelectedText获取或设置当前组合框中选定项的文本Sorted指示是否对组合框中的项进行排序addItem添加一个下拉选项addItems添加多个下拉选项currentIndex返回当前的下拉选项索引currentText返回当前下拉选项文本count返回下拉列表框中全部选项的个数removeItem删除选项clear清空所有选项insertItem将选项添加到指定的index位置setCurrentIndex显示指定index位置的选项【提示】在qtdesigner中可以添加下拉框选择内容self.ui.comboBox.addItem("苹果")#添加单个选项self.ui.comboBox.addItems(["葡萄","香蕉","西瓜"])#添加多个选项12self.Settings_UI.comboBox.textActivated.connect(self.check_result)#下拉框defcheck_result(self):data=self.Settings_UI.comboBox.currentText()self.Settings_UI.lineEdit_2.setText(data)#将选择的结果生成在LineEdit里12345self.comboBox.activated.connect(self.getText())#当用户在组合下拉框中选中一个条目时发射此信号,索引作为参数传递self.comboBox.currentIndexChanged.connect(self.box_result)#索引变化时触发绑定槽函数self.comboBox.currentTextChanged.connect(self.box_result)#文本变化时触发self.comboBox.highlighted.connect(self.box_result)#当用户高亮(光标移入或键盘选择)了弹出菜单中的某一条目时发射此信号self.comboBox.textActivated.connect(self.box_result)#当用户选择了条目之一时,发射此信号并将文本作为参数传递self.comboBox.textHighlighted.connect(self.box_result)#当用户高亮了弹出菜单中的某一条目时发射此信号二、按钮+列表文本显示self.Settings_UI.pushButton_7.clicked.connect(self.getItem)defgetItem(self):items=('OTU0','OTU1','OTU2')#返回两个值,第一个item代表用户选择的值,ok代表用户是否按下了取消item,ok=QInputDialog.getItem(self,'请选择','',items)ifokanditem:self.Settings_UI.lineEdit.setText(item)1234567QInputDialog.getItem:选择选项的下拉列表QInputDialog.getText:获取输入的文本内容QInputDialog.getInt:获取输入数字的计时器importsysfromPyQt5.QtWidgetsimport*fromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*classQInputDialogDemo(QWidget):def__init__(self):super(QInputDialogDemo,self).__init__()self.initUI()definitUI(self):self.setWindowTitle("输入对话框")layout=QFormLayout()self.button1=QPushButton("获取列表中的选项")self.button1.clicked.connect(self.getItem)self.lineEdit1=QLineEdit()layout.addRow(self.button1,self.lineEdit1)self.button2=QPushButton("获取字符串")self.button2.clicked.connect(self.getText)self.lineEdit2=QLineEdit()layout.addRow(self.button2,self.lineEdit2)self.button3=QPushButton("获取整数")self.button3.clicked.connect(self.getInt)self.lineEdit3=QLineEdit()layout.addRow(self.button3,self.lineEdit3)self.setLayout(layout)defgetItem(self):items=('C','C++','Ruby','Python','Java')#返回两个值,第一个item代表用户选择的值,ok代表用户是否按下了取消item,ok=QInputDialog.getItem(self,"请选择编程语言","语言列表",items)ifokanditem:self.lineEdit1.setText(item)defgetText(self):text,ok=QInputDialog.getText(self,"文本输入框","输入姓名")ifokandtext:self.lineEdit2.setText(text)defgetInt(self):num,ok=QInputDialog.getInt(self,"整数输入框","输入数字")ifokandnum:self.lineEdit3.setText(str(num))if__name__=='__main__':app=QApplication(sys.argv)main=QInputDialogDemo()main.show()sys.exit(app.exec_())1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253信号改变importsysfromPySide6.QtWidgetsimportQApplication,QMainWindow,QLineEditclassMainWindow(QMainWindow):def__init__(self):super(MainWindow,self).__init__()self.setWindowTitle("MyApp")widget=QLineEdit()'''设置输入内容最大长度'''widget.setMaxLength(10)'''设置文本框提示'''widget.setPlaceholderText("Enteryourtext")'''设置为只读模式'''#widget.setReadOnly(True)widget.returnPressed.connect(self.return_pressed)widget.selectionChanged.connect(self.selection_changed)#只要改变了,这个信号就会被触发widget.textChanged.connect(self.text_changed)#当修改文本内容时,这个信号就会被触发widget.textEdited.connect(self.text_edited)#当编辑文本结束时,这个信号就会被触发self.setCentralWidget(widget)defreturn_pressed(self):print("按了Enter键!")self.centralWidget().setText("BOOM!")defselection_changed(self):print("选中了文本!")print(self.centralWidget().selectedText())deftext_changed(self,s):print("文本改变了!")print(s)deftext_edited(self,s):print("文本被编辑了!")print(s)if__name__=='__main__':app=QApplication(sys.argv)#初始化win=MainWindow()#初始化界面并展示win.show()app.exec()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
|
|