一、PyQt5输入控件介绍
在PyQt5中,常用的输入控件有QLineEdit、QTextEdit和QComboBox。
1.QLineEdit
QLineEdit是一个用于单行文本输入的控件,可以接收用户的文本输入,并触发相应的事件。下面是一个简单的例子,演示了如何创建一个QLineEdit,并设置它的默认文本和占位符:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = QLabel('Name:')
self.edit = QLineEdit('John Doe')
self.edit.setPlaceholderText('Enter your name')
vbox = QVBoxLayout()
vbox.addWidget(label)
vbox.addWidget(self.edit)
self.setLayout(vbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
在上述代码中,我们创建了一个QLabel用于显示文本"Name:",并创建了一个QLineEdit用于接收用户的输入。我们还通过setPlaceholderText方法设置了QLineEdit的占位符,当QLineEdit为空时会显示该占位符。最后,我们将QLabel和QLineEdit添加到一个QVBoxLayout中,用于在窗口中布局。
2.QTextEdit
QTextEdit是一个用于多行文本输入的控件,可以接收用户的文本输入,并触发相应的事件。下面是一个简单的例子,演示了如何创建一个QTextEdit并设置它的默认文本和字体:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.edit = QTextEdit('Hello world')
self.edit.setFontFamily('Arial')
self.edit.setFontPointSize(12)
vbox = QVBoxLayout()
vbox.addWidget(self.edit)
self.setLayout(vbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
在上述代码中,我们创建了一个QTextEdit用于接收用户的输入,并设置了它的默认文本为"Hello world"。我们还通过setFontFamily和setFontPointSize方法设置了QTextEdit的字体。最后,我们将QTextEdit添加到一个QVBoxLayout中,用于在窗口中布局。
3.QComboBox
QComboBox是一个用于下拉菜单选择的控件,可以让用户从一组预定义的选项中进行选择,并触发相应的事件。下面是一个简单的例子,演示了如何创建一个QComboBox并设置它的选项:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QVBoxLayout
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = QLabel('City:')
self.combo = QComboBox()
self.combo.addItem('Beijing')
self.combo.addItem('Shanghai')
self.combo.addItem('Guangzhou')
self.combo.addItem('Shenzhen')
vbox = QVBoxLayout()
vbox.addWidget(label)
vbox.addWidget(self.combo)
self.setLayout(vbox)
self.combo.currentIndexChanged.connect(self.onComboChanged)
def onComboChanged(self, index):
city = self.combo.currentText()
print(f'Selected city: {city}')
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
在上述代码中,我们创建了一个QComboBox用于让用户选择城市,通过addItem方法设置了选项列表,并通过currentIndexChanged信号连接了onComboChanged槽函数。当用户选择了一个选项时,onComboChanged槽函数会被调用,获取用户选择的城市并打印出来。最后,我们将QLabel和QComboBox添加到一个QVBoxLayout中,用于在窗口中布局。
二、更多详细教程参考
- QLineEdit单行文本框方法大全和信号
- QValidator文本内容限定方法和验证器
- PyQt5 setInputMask字符掩码详细讲解
- QLineEdit控件setText()和text()方法
- QLineEdit控件clear()和setMaxLength()方法
- QLineEdit控件setReadOnly()和setPlaceholderText()方法
- QLineEdit控件setInputMask()和setValidator()方法
- QLineEdit控件cursorPosition()和setCursorPosition()方法
- QLineEdit控件selectedText()和setTextMargins()方法
- QLineEdit控件setMaxLength()和undo()方法
- QLineEdit控件redo()和paste()方法
- QLineEdit控件setPlaceholderText()和setReadOnly()方法
- QLineEdit控件setInputMask()和setText()方法
- QFrame多行文本输入框边框基类API大全
- QAbstractScrollArea多行文本输入框滚动条基类API大全
- PyQt5 QTextEdit多行文本输入框API大全
- QTextCursor多行文本框文本光标插入文本
- PyQt5 QTextCursor多行文本框文本光标文本块和字符格式设置
- QTextEdit多行文本框对文本操作
- QPlainTextEdit大文本编辑框API大全
- PyQt5 QKeySequenceEdit API大全