分類彙整: PyQt5

使用python與PyQt5製作登入UI畫面

在PyQt5中,使用QLineEdit來模擬製作帳號密碼登入UI,首先使用Qt designer來拉出元件
如以下畫面所示,拉出Label、QLineEdit、QCheckBox與QpushButton,同時在designer中設定QCheckBox與QPushButton的信號-槽

 

  1. QCheckBox搭配show_pw訊號,用來控制當勾選此checkBox時,就顯示密碼
  2. QPushButton搭配login_test訊號,當點擊時就開始進行帳號密碼的登入測試,這邊只印出log,沒有實際的進行實質的登入
  3. 密碼可見與不可見,使用QLineEdit原生的setEchoMode與QLineEdit.Password進行設定
  4. PasswordEchoOnEdit效果不好,必須等當前QLineEdit失去焦點才能將密碼不可見,在簡單的UI元件下,不好控制,除非再額外新增相關焦點控制

 

範例程式:

 

UI檔案

 

最終UI畫面效果如下,包含密碼可見與不可見

勾選show checkbox則密碼可見

 

預設密碼不可見

PyQT5: QTextEdit 選取文字測試

利用QTextCursor取得指針位置,修改選取參數查看顯示效果

QTextCursor選取參數:

  • Document 3 Selects the entire document. 選取全部
  • BlockUnderCursor 2 Selects the block of text under the cursor. 選取到cursor位置的block
  • LineUnderCursor 1 Selects the line of text under the cursor. 選取與cursor位置同一行
  • WordUnderCursor 0 Selects the word under the cursor. 選取cursor位置的單字

EX: Document

 

全部選取效果(Document)

依序測試其他效果
BlockUnderCursor

LineUnderCurosr

WordUnderCursor