QLineEdit 单行输入框
QLineEdit 是 PyQt6 里的单行输入框组件,这篇教学会介绍如何在 PyQt6 窗口里加入 QLineEdit 单行输入框,并实作修改样式以及读取输入文字等基本应用。
快速预览:
- 加入 QLineEdit 单行输入框
- QLineEdit位置设置
- QLineEdit 样式设定
- QLineEdit常用方法
- 取得 QLineEdit 输入字内容
加入 QLineEdit 单行输入框
建立 PyQt6 窗口物件后,透过 QtWidgets.QLineEdit(widget)方法,就能在指定的组件中建立单行输入框组件,下方的程序执行后,会在窗口里加入一个单行输入框。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛编程思维')
Form.resize(300, 200)
input = QtWidgets.QLineEdit(Form) # 建立單行輸入框
input.setGeometry(20,20,100,20) # 設定位置和尺寸
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('千牛编程思维')
self.resize(300, 200)
self.ui()
def ui(self):
self.input = QtWidgets.QLineEdit(self) # 建立單行輸入框
self.input.setGeometry(20,20,100,20) # 設定位置和尺寸
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QLineEdit位置设置
透过下列 QLineEdit 方法,可以将 QLineEdit 组件定位到指定的位置:
| 方法 | 参数 | 说明 |
|---|---|---|
| move() | x, y | 设定 QLineEdit 在摆放的父组件中的 xy 坐标,x 往右为正,y 往下为正,尺寸根据内容自动延伸。 |
| setGeometry() | X,X,Y,W,H | 设定 QLineEdit 在摆放的父组件中的 xy 坐标和长宽尺寸,x 往右为正,y 往下为正,如果超过长宽尺寸,输入的文字会被裁切无法显示。 |
下方的程序执行后会放入两个 QLineEdit,一个使用 move() 定位并使用预设宽度,另外一个使用 setGeometry() 方法定位。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛编程思维')
Form.resize(300, 200)
input_1 = QtWidgets.QLineEdit(Form) # 第一個輸入框
input_1.move(20,20)
input_2 = QtWidgets.QLineEdit(Form) # 第二個輸入框
input_2.setGeometry(20,50,100,20)
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('千牛编程思维')
self.resize(300, 200)
self.ui()
def ui(self):
self.input_1 = QtWidgets.QLineEdit(self) # 第一個輸入框
self.input_1.move(20,20)
self.input_2 = QtWidgets.QLineEdit(self) # 第二個輸入框
self.input_2.setGeometry(20,50,100,20)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QLineEdit 样式设定
透过 setStyleSheet(),可以使用类似网页的 CSS 语法设定 QPushButton 样式,下方的程序执行后,第一个输入框会套用 CSS 样式语法,当输入框为焦点时,会变成黄底红框的样式,而第二个输入框则维持原本的样式。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛编程思维')
Form.resize(300, 200)
input_1 = QtWidgets.QLineEdit(Form)
input_1.move(20,20)
input_1.setStyleSheet('''
QLineEdit {
border:1px solid #000;
}
QLineEdit:focus {
border:2px solid #f00;
background:#ff0;
}
''')
input_2 = QtWidgets.QLineEdit(Form)
input_2.setGeometry(20,50,100,20)
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('千牛编程思维')
self.resize(300, 200)
self.ui()
def ui(self):
self.input_1 = QtWidgets.QLineEdit(self)
self.input_1.move(20,20)
self.input_1.setStyleSheet('''
QLineEdit {
border:1px solid #000;
}
QLineEdit:focus {
border:2px solid #f00;
background:#ff0;
}
''')
self.input_2 = QtWidgets.QLineEdit(self)
self.input_2.setGeometry(20,50,100,20)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QLineEdit常用方法
下方列出使用 QLineEdit 的常用方法:
| 方法 | 参数 | 说明 |
|---|---|---|
| setText() | str | 预设输入的文字内容。 |
| setReadOnly() | bool | 设定只能读取,预设 False。 |
| setDisabled() | bool | 设定是否禁用,预设 False。 |
| setMaxLength() | int | 输入的最大字元数。 |
| setFocus() | 设定为焦点。 | |
| setEchoMode() | mode | 设定QtWidgets.QLineEdit.EchoMode.Password表示为密码,看不见输入内容 ( 此处方法与 PyQt5 不同 )。 |
| textChanged.connect() | fn | 文字改变时要执行的函数。 |
| text() | 取得输入框内容。 |
下方的程序执行后,预设会先点击第二个输入框,而第一个输入框最多只能输入五个字元,并且采用密码的型态表现。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛编程思维')
Form.resize(300, 200)
input_1 = QtWidgets.QLineEdit(Form)
input_1.setGeometry(20,20,100,20)
input_1.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
input_1.setText('12345')
input_1.setMaxLength(5)
input_2 = QtWidgets.QLineEdit(Form)
input_2.setGeometry(20,50,100,20)
input_2.setFocus()
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('千牛编程思维')
self.resize(300, 200)
self.ui()
def ui(self):
self.input_1 = QtWidgets.QLineEdit(self)
self.input_1.setGeometry(20,20,100,20)
self.input_1.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
self.input_1.setText('12345')
self.input_1.setMaxLength(5)
self.input_2 = QtWidgets.QLineEdit(self)
self.input_2.setGeometry(20,50,100,20)
self.input_2.setFocus()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
取得 QLineEdit 输入字内容
运用textChanged.connect(fn)方法,就能在输入框内容改变时,执行特定的函数,下方的程序执行后,当单行输入框的内容发生改变,就会透过 QLabel 显示输入的内容。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛编程思维')
Form.resize(300, 200)
def show():
label.setText(input.text())
input = QtWidgets.QLineEdit(Form)
input.setGeometry(20,20,100,20)
input.textChanged.connect(show) # 文字改變時執行函数
label = QtWidgets.QLabel(Form)
label.setGeometry(20,50,100,20)
Form.show()
sys.exit(app.exec())
class 写法 ( 注意不能使用 show 作为方法名称,会覆写基类的 show 方法造成无法显示 ):
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('千牛编程思维')
self.resize(300, 200)
self.ui()
def ui(self):
self.input = QtWidgets.QLineEdit(self)
self.input.setGeometry(20,20,100,20)
self.input.textChanged.connect(self.showText) # 文字改變時執行函数
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(20,50,100,20)
def showText(self):
self.label.setText(self.input.text())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())