PyQt5 控件字體樣式等設置

<div class="title_1">1、API接口設置</div> - 好比我這段代碼中的一些設置,設置文字、居中、禁止複製、LineEdit輸入爲password等等html

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFrame
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QApplication




from View import interface

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow,self).__init__(None)
        self.setWindowTitle("對金屬腐蝕性試驗儀")
        self.initUI()

    def initUI(self):
        layout = QGridLayout()
        layout.setSpacing(10)
        self.loginLabel = QLabel("用戶名:")
        self.loginLabel.setAlignment(Qt.AlignRight)
        self.loginLabel.setStyleSheet("color:rgb(20,20,20,255);font-size:16px;font-weight:bold:text")
        self.loginTxt = QLineEdit()
        self.loginTxt.setText("admin")
        self.loginTxt.setPlaceholderText("User Name")
        self.loginTxt.setClearButtonEnabled(True)
        self.pwdLabel = QLabel("密碼:")
        self.pwdLabel.setAlignment(Qt.AlignRight)
        self.pwdTxt = QLineEdit()
        self.pwdTxt.setContextMenuPolicy(Qt.NoContextMenu) #禁止複製粘貼
        self.pwdTxt.setPlaceholderText("Password")
        self.pwdTxt.setText("admin")
        self.pwdTxt.setEchoMode(QLineEdit.Password)
        self.pwdTxt.setClearButtonEnabled(True)
        self.registeredBtn = QPushButton("註冊")
        self.loginBtn = QPushButton("登錄")

        self.headLabel = QLabel("用戶登錄")
        self.headLabel.resize(300,30)
        self.headLabel.setAlignment(Qt.AlignCenter)
        self.headLabel.setStyleSheet("color:rgb(10,10,10,255);font-size:25px;font-weight:bold;font-family:Roman times;")

        self.headLabel.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
        layout.addWidget(self.headLabel,0,0,1,2)
        policy = self.headLabel.sizePolicy()
        print(policy.verticalPolicy())
        policy.setVerticalPolicy(1)
        print(policy.verticalPolicy())
        # policy.setVerticalPolicy(1)
        layout.addWidget(self.loginLabel,1,0)
        layout.addWidget(self.loginTxt,1,1)
        layout.addWidget(self.pwdLabel,2,0)
        layout.addWidget(self.pwdTxt,2,1)
        layout.addWidget(self.registeredBtn,3,0)
        layout.addWidget(self.loginBtn,3,1)

        frame = QFrame(self)
        frame.setLayout(layout)
        self.setCentralWidget(frame)
        self.resize(300,150)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    mainWindow.activateWindow()
    mainWindow.raise_()
    app.exec_()
    del mainWindow
    del app

<div class="title_3">1.1.0 QLineEdit一些屬性</div>app

inputMask設置掩碼 text 設置文本 maxLength文本框輸入的最大字符數 frame 設置邊框 echoMode 設置文本框顯示格式 Normal正常顯示所輸入的字符,此爲默認選項 NoEcho不顯示任何輸入的字符,經常使用於密碼類型的輸入,且長度保密 Password顯示與平臺相關的密碼掩飾字符,而不是實際輸入的字符 PasswordEchoOnEdit在編輯時顯示字符,負責顯示密碼類型的輸入 cursorPosition光標位置 alignment文本對齊方式 AlignLeft左對齊 AlignRight右對齊 AlignCenter水平居中對齊 AlignJustify水平方向調整間距兩端對齊 AlignTop垂直上對齊 AlignBottom垂直方下對齊 AlignVCenter垂直方向居中對齊 dragEnabled設置文本框是否接受拖動 readOnly設置文本爲只讀 placeholderText設置文本框提示文字 cursorMoveStyle光標移動風格 LogicalMoveStyle邏輯風格 VisualMoveStyle視覺風格 clearButtonEnabled快速刪除按鈕字體

<a style="color:red;font-size:20px;" href="https://zhuanlan.zhihu.com/p/57146150" target="_blank">參考文章,QLineEdit屬性、信號、方法等</a>code

<div class="title_2">1.1 經常使用的一些設置</div>orm

  • setAlignment(self, Union, Qt_Alignment=None,Qt_AlignmentFlag=None): 設置字體對齊方式
參數 做用
AlignAbsolute=16
AlignBaseline=256
AlignBottom=64 底端對齊
AlignCenter=132 徹底居中
AlignHCenter=4 水平居中
AlignHorizontal_Mask=31
AlignJustify=8 可用空間對齊
AlignLeading=1 領頭對齊(理解爲左對齊吧)
AlignLeft=1 左對齊
AlignRight=2 右對齊
AlignTop=32 上對齊
AlignTrailing=2 尾對齊(右對齊
AlignVCenter=128 垂直居中
  • setClearButtonEnabled(self, bool): 是否有清除文本按鈕(如我第一段程序文本框後的 小黑X)
  • setCompleter(self, QCompleter):設置自動補全<a href="https://www.cnblogs.com/shiqi17/p/12176497.html" target="_blank">QLineEdit自動補全</a>

  • setCursorMoveStyle(self, Qt_CursorMoveStyle):
  • setCursorPosition(self, p_int):
  • setDragEnabled(self, bool):
  • setEchoMode(self, QLineEdit_EchoMode):
  • setFrame(self, bool):
  • setInputMask(self, p_str):
  • setMaxLength(self, p_int):
  • setModified(self, bool):
  • setPlaceholderText(self, p_str):
  • setReadOnly(self, bool):
  • setSelection(self, p_int, p_int_1):
  • setText(self, p_str):
  • setTextMargins(self, *__args):
  • setValidator(self, QValidator):
相關文章
相關標籤/搜索