1、QLineEdit介紹
是一個單行文本編輯器;
允許用戶輸入和編輯單行純文本;
自帶一組編輯功能:撤銷、重做、剪切、粘貼、拖放;
繼承於QWidget


2、控件的創建、文本的設置和獲取
(1)框架


(2)功能操作和案例
1 import sys 2 from PyQt5.Qt import * 3 4 app=QApplication(sys.argv) 5 6 window=QWidget() 7 window.setWindowTitle("QLineEdit") 8 window.resize(500,500) 9 10 # le = QLineEdit(window) 11 le = QLineEdit('用戶',window) # 文本框里顯示“用戶” 12 13 # 文本的設置和獲取 14 le.setText('哈哈') 15 le.insert('18') # 在光標處插入 16 print(le.text()) # 獲取 17 18 btn = QPushButton(window) 19 btn.setText('按鈕') 20 btn.move(100,100) 21 # btn.pressed.connect(lambda :le.insert('明天')) 22 # btn.pressed.connect(lambda :print(le.text())) 23 btn.pressed.connect(lambda :print(le.displayText())) # 獲取顯示的內容 24 25 window.show() 26 sys.exit(app.exec_())
1 import sys 2 from PyQt5.Qt import * 3 4 app=QApplication(sys.argv) 5 6 window=QWidget() 7 window.setWindowTitle("QLineEdit") 8 window.resize(500,500) 9 10 le_a = QLineEdit(window) 11 le_a.move(100,50) 12 13 le_b = QLineEdit(window) 14 le_b.move(100,100) 15 16 copy_btn = QPushButton(window) 17 copy_btn.setText("復制") 18 copy_btn.move(100,150) 19 copy_btn.pressed.connect(lambda :le_b.setText(le_a.text())) 20 21 window.show() 22 sys.exit(app.exec_())
3、輸出模式
(1)框架

(2)功能操作
1 # *******************輸出模式**********************開始 2 import sys 3 from PyQt5.Qt import * 4 5 app=QApplication(sys.argv) 6 7 window=QWidget() 8 window.setWindowTitle("輸出模式") 9 window.resize(500,500) 10 11 le_a = QLineEdit(window) 12 le_a.move(100,50) 13 14 le_b = QLineEdit(window) 15 le_b.move(100,100) 16 17 # le_b.setEchoMode(QLineEdit.NoEcho) # 正常輸入,但不顯示 18 # le_b.setEchoMode(QLineEdit.Normal) # 默認顯示 19 # le_b.setEchoMode(QLineEdit.Password) # 密文模式 20 le_b.setEchoMode(QLineEdit.PasswordEchoOnEdit) # 編輯時明文,結束時密文 21 22 window.show() 23 sys.exit(app.exec_()) 24 # *******************輸出模式**********************結束
(3)案例:模擬用戶登錄
創建一個窗口,添加兩文本框和一個按鈕;
一個用作賬號,一個用作密碼;
點擊登錄按鈕,獲取賬號和密碼信息;
進行比對賬號密碼信息;
1 # *********登錄案例***********begin 2 from PyQt5.Qt import * 3 4 class AccountTool: 5 ACCOUNT_ERROR = 1 6 PWD_ERROR = 2 7 SUCCESS = 3 8 @staticmethod 9 def check_login(account,pwd): 10 if account != 'XB': 11 return AccountTool.ACCOUNT_ERROR 12 if pwd != 'likeit123': 13 return AccountTool.PWD_ERROR 14 return AccountTool.SUCCESS 15 16 class Window(QWidget): 17 def __init__(self): 18 super().__init__() 19 self.setWindowTitle("登錄案例") 20 self.resize(500, 500) 21 self.setMinimumSize(400,400) # 設置窗口的最小尺寸 22 self.setMaximumSize(800,800) # 設置窗口的最大尺寸 23 self.setup_ui() 24 25 def setup_ui(self): 26 # 添加三個控件 27 self.account_le = QLineEdit(self) 28 self.pwd_le = QLineEdit(self) 29 self.pwd_le.setEchoMode(QLineEdit.Password) 30 self.login_btn = QPushButton(self) 31 self.login_btn.setText('登錄') 32 33 # 添加兩個標簽 34 self.label1 = QLabel(self) 35 self.label1.setText('賬號輸入錯誤') 36 self.label2 = QLabel(self) 37 self.label2.setText('密碼輸入錯誤') 38 39 # 設置標簽默認不可見 40 self.label1.setVisible(False) 41 self.label2.setVisible(False) 42 43 # 如沒有輸入賬號,登錄按鈕不可用 44 self.login_btn.setEnabled(False) 45 46 self.account_le.textChanged.connect(self.text_change) 47 48 self.login_btn.clicked.connect(self.login_cao) 49 50 def text_change(self,text): 51 self.login_btn.setEnabled(len(text) > 0) 52 53 def login_cao(self): 54 # print('xxx',self) 55 # 獲取賬號和密碼信息 56 account = self.account_le.text().upper() 57 pwd = self.pwd_le.text() 58 59 state = AccountTool.check_login(account,pwd) 60 if state == AccountTool.ACCOUNT_ERROR: 61 print('賬號錯誤') 62 self.label1.setVisible(True) 63 self.account_le.setText('') 64 self.pwd_le.setText('') 65 self.account_le.setFocus() # 設置獲取焦點 66 return None 67 if state == AccountTool.PWD_ERROR: 68 print('密碼錯誤') 69 self.label2.setVisible(True) 70 self.pwd_le.setText('') 71 self.pwd_le.setFocus() # 設置獲取焦點 72 return None 73 if state == AccountTool.SUCCESS: 74 print('登陸成功') 75 76 77 def resizeEvent(self, evt): 78 widget_w = 150 79 widget_h = 40 80 margin = 60 81 label_h = 10 82 83 self.account_le.resize(widget_w,widget_h) 84 self.pwd_le.resize(widget_w,widget_h) 85 self.login_btn.resize(widget_w,widget_h) 86 # 標簽大小 87 self.label1.resize(widget_w,widget_h) 88 self.label2.resize(widget_w,widget_h) 89 90 x = (self.width() - widget_w)/2 91 92 self.account_le.move(x,self.height()/4) 93 self.pwd_le.move(x,self.account_le.y()+widget_h+margin) 94 self.login_btn.move(x,self.pwd_le.y()+widget_h+margin) 95 # 標簽位置 96 self.label1.move(x,self.account_le.y()+widget_h+label_h) 97 self.label2.move(x,self.pwd_le.y()+widget_h+label_h) 98 99 if __name__ == '__main__': 100 import sys 101 102 app=QApplication(sys.argv) 103 104 window=Window() 105 window.show() 106 sys.exit(app.exec_()) 107 # *********登錄案例***********end
4.占位提示字符串
(1)框架

(2)功能操作與結果展示
1 # 占位文本的提示 2 self.account_le.setPlaceholderText("請輸入賬號") 3 self.pwd_le.setPlaceholderText("請輸入密碼")

5、清空按鈕顯示
(1)框架‘’

(2)功能操作及結果顯示
# 設置密碼文本框,自動現實是清空按鈕 self.pwd_le.setClearButtonEnabled(True)

6、添加操作行為
(1)框架

(2)功能操作及結果顯示
1 # 添加自定義行為操作(明文和密文切換) 2 action = QAction(self.pwd_le) 3 action.setIcon(QIcon('close.png')) 4 5 def change(): 6 print('改變明文和密文') 7 if self.pwd_le.echoMode() == QLineEdit.Normal: # 判斷輸出模式 8 self.pwd_le.setEchoMode(QLineEdit.Password) 9 action.setIcon(QIcon('close.png')) 10 else: 11 self.pwd_le.setEchoMode(QLineEdit.Normal) 12 action.setIcon(QIcon('open.png')) 13 14 action.triggered.connect(change) 15 16 self.pwd_le.addAction(action,QLineEdit.TrailingPosition) # 添加在末尾處 17 # self.pwd_le.addAction(action,QLineEdit.LeadingPosition) # 添加在開

7、自動補全
(1)框架

(2)功能操作及結果展示
1 # 自動補全 2 completer = QCompleter(['xb','feng','xing','feel'],self.account_le) 3 self.account_le.setCompleter(completer)

8、輸入限制
(1)框架



(2)功能操作及結果展示
①長度限制與只讀限制
1 # 最大長度限制 2 le_a.setMaxLength(5) 3 print(le_a.maxLength()) 4 5 # 只讀限制 6 le_a.setReadOnly(True) 7 le_a.setText("王炸,要不起!")

② 驗證器
1 # *******************驗證器的使用**********************開始 2 from PyQt5.Qt import * 3 4 class AgeValidator(QValidator): 5 def validate(self, input_str, pos_int): 6 print(input_str,pos_int) 7 # 判定字符串,應該全部都是由一些數字組成 8 try: 9 if 18 <= int(input_str) <= 180: 10 return (QValidator.Acceptable,input_str,pos_int) # 驗證通過 11 elif 1 <= int(input_str) <= 17: 12 return (QValidator.Intermediate,input_str, pos_int) # 暫時不判斷 13 else: 14 return (QValidator.Invalid, input_str, pos_int) # 驗證不通過 15 except: 16 if len(input_str) == 0: 17 return (QValidator.Intermediate, input_str, pos_int) 18 return (QValidator.Invalid,input_str,pos_int) 19 20 def fixup(self, p_str): 21 print('xxx',p_str) 22 try: 23 if int(p_str) <18: 24 return '18' 25 return '180' 26 except: 27 return '18' 28 29 class MyValidator(QIntValidator): 30 def fixup(self, p_str): 31 if len(p_str) == 0 or int(p_str) < 18: 32 return '18' 33 return '180' 34 35 36 class Window(QWidget): 37 def __init__(self): 38 super().__init__() 39 self.setWindowTitle("驗證器的使用") 40 self.resize(500, 500) 41 self.setup_ui() 42 43 def setup_ui(self): 44 le = QLineEdit(self) 45 le.move(100,100) 46 47 le2 = QLineEdit(self) 48 le2.move(200,200) 49 50 # 只能輸入18 - 180才能顯示 51 # 法一 52 # validator = AgeValidator() 53 54 # 法二 55 validator = MyValidator(18,180) 56 57 le.setValidator(validator) 58 59 if __name__ == '__main__': 60 import sys 61 62 app=QApplication(sys.argv) 63 64 window=Window() 65 window.show() 66 sys.exit(app.exec_()) 67 # *******************驗證器的使用**********************結束

③ 掩碼
1 # le_b 設置掩碼 2 # 總共輸入5位, 左邊2(必須是大寫字母)- 右邊2(必須是數字) 3 le_b.setInputMask(">AA-99;#") # 改為#,默認是空格

9、是否被編輯
(1)框架

(2)功能操作與展示
1 print(le_b.isModified()) # 文本未編輯:False 編輯后:True 2 le_b.setModified(False)
10、光標控制
(1)框架

(2)功能操作及展示
1 le.cursorBackward(False,2) # 光標向左2個字符且不選中 2 le.cursorBackward(True,2) # 光標向左2個字符且選中 3 le.cursorForward(True,2) # 光標向右2個字符且選中 4 le.cursorWordBackward(True) # 向左移動一個單詞(空格區分) 5 le.cursorWordForward(True) # 向右移動一個單詞(空格區分) 6 le.home(True) # 光標移到開頭並選中 7 le.end(False) # 光標移到末尾且不選中 8 le.setCursorPosition(len(le.text())/2) # 設置光標位置 9 print(le.cursorPosition()) 10 print(le.cursorPositionAt(QPoint(55,5))) # 獲取指定坐標位置對應文本光標位置 11 le.setFocus() # 焦點,方便觀察上述操作
11、文本邊距設置
(1)框架

(2)功能操作及展示
1 le = QLineEdit(window) 2 le.move(100,100) 3 le.resize(300,300) 4 # le.setContentsMargins(100,0,0,0) # 內容外邊距 5 le.setStyleSheet('background-color:cyan') 6 le.setTextMargins(100,200,0,0) # 設置文本外邊距(可視區域內移動文本)
12、對齊方式
(1)框架

(2)功能操作及展示
1 # 文本水平靠右,垂直方向靠下 2 le.setAlignment(Qt.AlignRight | Qt.AlignBottom) 3 # 文本水平居中 4 le.setAlignment(Qt.AlignCenter)
13、常用編輯功能
(1)框架

(2)功能操作及展示
① 退格、刪除、清空、復制、剪切、粘貼、撤銷、拖放
1 # le.backspace() # 刪除 2 # le.del_() # 刪除選中文本或刪除光標右側的一個字符 3 # le.clear() # 4 le.cursorBackward(True,3) 5 # le.copy() # 復制 6 le.cut() # 剪切 7 le.setCursorPosition(0) 8 le.paste() # 粘貼 9 10 le.setFocus() # 焦點,方便觀察上述操作 11 12 le.setDragEnabled(True) # 支持拖拽
① 文本選中
1 # le.setSelection(2,10) # 選中第2個字符到第10個字符 2 le.selectAll() # 等同於 le.setSelection(0,len(le.text())) 3 # le.deselect() # 取消選中 4 print(le.hasSelectedText()) # 判斷是否選中 5 print(le.selectedText()) # 獲取選中的內容 6 print(le.selectionStart()) # 獲取選中的開始位置 7 print(le.selectionEnd()) # 獲取選中的末尾位置 8 print(le.selectionLength()) # 獲取選中的長度
14.信號
(1)框架

(2)信號操作及顯示
1 # *******************信號**********************開始 2 import sys 3 from PyQt5.Qt import * 4 5 app=QApplication(sys.argv) 6 7 window=QWidget() 8 window.setWindowTitle("信號") 9 window.resize(500,500) 10 11 le = QLineEdit(window) 12 le.move(200,50) 13 le.resize(100,100) 14 15 le1 = QLineEdit(window) 16 le1.move(200,300) 17 18 le.textEdited.connect(lambda val:print('文本框編輯的時候:',val)) # 指用戶編輯的時候 19 le.textChanged.connect(lambda val:print('文本框內容發生改變的時候:',val)) # 無論用戶還是開發者,都發生 20 # le.returnPressed.connect(lambda :le1.setFocus()) # 切換焦點到文本框le1中 21 # le.editingFinished.connect(lambda :print('結束編輯')) 22 le.cursorPositionChanged.connect(lambda old_Pos, new_Pos:print(old_Pos,new_Pos)) # 監聽光標的移動位置 23 le.selectionChanged.connect(lambda :print("選中文本發生改變",le.selectedText())) 24 25 window.show() 26 sys.exit(app.exec_()) 27 # *******************信號**********************結束

