Python PyQt5 QLineEdit 文字輸入框用法與範例

本篇 ShengYu 介紹 Python PyQt5 QLineEdit 用法與範例,Python GUI 程式設計裡文字輸入框的處理是很基本且常用的功能,接下來就來學習怎麼用 PyQt5 建立 QLineEdit 吧!

以下的 Python PyQt5 QLineEdit 用法與範例將分為這幾部分,

  • PyQt5 QLineEdit 基本用法
  • PyQt5 取得 QLineEdit 的輸入文字
  • PyQt5 QLineEdit 限制只能輸入數字
  • PyQt5 QLineEdit 使用替代符號隱藏輸入密碼

PyQt5 QLineEdit 基本用法

這邊示範 PyQt5 QLineEdit 基本用法,QLineEdit() 初始化完以後,先用 move() 移到想要顯示的座標位置,如下例中的 mylabel 移到左邊 (10,10) 座標,mylineedit 移到右邊 (60,10) 座標,這樣的手動排版比較土炮一點,下個範例會介紹怎麼用 QGridLayout 來進行 UI 排版,接下來就使用 self.show() 就可以完成初步的 PyQt5 QLineEdit 體驗囉!範例如下,

python-pyqt-qlineedit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLineEdit, QLabel)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 60)

self.mylabel = QLabel('Name:', self)
self.mylabel.move(10, 10)

self.mylineedit = QLineEdit(self)
self.mylineedit.move(60, 10)

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

PyQt5 取得 QLineEdit 的輸入文字

PyQt5 取得 QLineEdit 的輸入文字可以用 QLineEdit.text() 的方式,如下例子中的 mylineedit.text(),這邊示範按下按鈕時將 QLineEdit 裡的文字取出來後,並且設定成 mybutton 顯示的文字,

python-pyqt-qlineedit2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel, QPushButton)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 80)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('Name:', self)
gridlayout.addWidget(self.mylabel, 0, 0)

self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)

self.mybutton = QPushButton('button', self)
gridlayout.addWidget(self.mybutton, 1, 0, 1, 2)
self.mybutton.clicked.connect(self.onButtonClick)

def onButtonClick(self):
#print(self.mylineedit.text())
if self.mylineedit.text() != '':
self.mybutton.setText(self.mylineedit.text())

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

這邊就使用 QGridLayout 來進行 UI 排版就不使用 move 了,QGridLayout.addWidget() 的 3 種函式參數介紹如下,

1
2
3
QGridLayout.addWidget(QWidget)
QGridLayout.addWidget(QWidget, int row, int column, Qt.Alignment alignment=0)
QGridLayout.addWidget(QWidget, int row, int column, int rowSpan, int columnSpan, Qt.Alignment alignment=0)

把 mylabel 放到 (0,0) 的位置,把 mylineedit 放到 (0,1) 的位置,把 mybutton 放到 (1,0) 的位置並且第 4 個引數 columnSpan 延伸為 2,

輸入文字且按下按鈕的結果圖如下,

這邊改用另一個例子,我們寫一個簡單的數學問題,讓使用者來回答,
取得 QLineEdit 的輸入文字後進行判斷,並且用 QMessageBox 來回應使用者,答案正確的話就用顯示”答對了”在 messagebox 上,

python-pyqt-qlineedit3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel, QPushButton, QMessageBox)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 80)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('1+1=', self)
gridlayout.addWidget(self.mylabel, 0, 0)

self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)

self.mybutton = QPushButton('完成', self)
gridlayout.addWidget(self.mybutton, 1, 0, 1, 2)
self.mybutton.clicked.connect(self.onButtonClick)

def onButtonClick(self):
#print(self.mylineedit.text())
if self.mylineedit.text() == '':
QMessageBox.about(self, 'message', '未輸入答案')
elif self.mylineedit.text() == '2':
QMessageBox.about(self, 'message', '答對了')
else:
QMessageBox.about(self, 'message', '答錯了')

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

關於 QMessageBox 的詳細用法可以看這篇介紹。

PyQt5 QLineEdit 限制只能輸入數字

這邊介紹蠻常使用到的功能,就是限制使用者只能在 QLineEdit 裡輸入數字,需要在 mylineedit 設定一個 QIntValidator() 它會去驗證輸入的文字是不是整數,本例中是限制使用者只能輸入數字,用法如下,

python-pyqt-qlineedit4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel)
from PyQt5.QtGui import QIntValidator

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 60)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('Name:', self)
gridlayout.addWidget(self.mylabel, 0, 0)

self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)
self.mylineedit.setValidator(QIntValidator()) # only int

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

QIntValidator() 還能限制你輸入的整數範圍,例如 0~255,以及限制使用者只能輸入 double,這方面的詳細內容可以看 PyQt5 QLineEdit 限制輸入數字這篇介紹。

PyQt5 QLineEdit 使用替代符號隱藏輸入密碼

這邊示範一個登入視窗,裡面有兩個 QLineEdit,分別是要讓使用者輸入使用者名稱與密碼,我們希望使用者輸入的密碼不要用明碼顯示出來,取而代之的是用替代符號來顯示,那要怎麼作呢?就是在 QLineEdit.setEchoMode() 裡設定 QLineEdit.Password 參數,這樣輸入時就會顯示替代符號而不會顯示明碼了,範例如下,

python-pyqt-qlineedit5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 60)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('Name:', self)
gridlayout.addWidget(self.mylabel, 0, 0)
self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)

self.mylabel2 = QLabel('Password:', self)
gridlayout.addWidget(self.mylabel2, 1, 0)
self.mylineedit2 = QLineEdit(self)
self.mylineedit2.setEchoMode(QLineEdit.Password)
gridlayout.addWidget(self.mylineedit2, 1, 1)

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

以上就是 Python PyQt5 QLineEdit 文字輸入框的介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!
下一篇將會介紹 PyQt5 QComboBox 下拉式選單用法與範例

其它相關文章推薦
Python QLabel 標籤用法與範例
Python QPushButton 按鈕用法與範例
Python 新手入門教學懶人包
Python PyQt5 新手入門教學