PyQt5 控件字体样式等设置的实现

 更新时间:2020年05月13日 10:30:28   作者:c_G-17  
这篇文章主要介绍了PyQt5 控件字体样式等设置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、API接口设置

比如我这段代码中的一些设置,设置文字、居中、禁止复制、LineEdit输入为password等等

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

1.1.0 QLineEdit一些属性

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快速删除按钮

参考文章,QLineEdit属性、信号、方法等

1.1 常用的一些设置

参数 作用
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):设置自动补全QLineEdit自动补全

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):

到此这篇关于PyQt5 控件字体样式等设置的实现的文章就介绍到这了,更多相关PyQt5 控件字体样式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python使用爬虫爬取贵阳房价的方法详解

    Python使用爬虫爬取贵阳房价的方法详解

    这篇文章主要为大家详细介绍了Python爬虫爬取贵阳房价的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • python中对列表的删除和添加方法详解

    python中对列表的删除和添加方法详解

    这篇文章主要为大家详细介绍了python中对列表的删除和添加方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Python shutil模块文件和目录操作示例详解

    Python shutil模块文件和目录操作示例详解

    本文将会学习到 shutil 模块,包括其主要功能和示例代码,以帮助你更好地理解如何使用它来处理文件和目录
    2023-11-11
  • Python3+Flask安装使用教程详解

    Python3+Flask安装使用教程详解

    这篇文章主要介绍了Python3+Flask安装使用教程详解,需要的朋友可以参考下
    2021-02-02
  • python列表字典排序的实现示例

    python列表字典排序的实现示例

    在Python中,对列表字典进行排序是一项常见的任务,本文主要介绍了python列表字典排序的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • pytorch简介

    pytorch简介

    Pytorch是torch的python版本,是由Facebook开源的神经网络框架,专门针对 GPU 加速的深度神经网络(DNN)编程。这篇文章给大家介绍pytorch的相关知识,感兴趣的朋友一起看看吧
    2020-11-11
  • python twilio模块实现发送手机短信功能

    python twilio模块实现发送手机短信功能

    这篇文章主要介绍了python twilio模块实现发送手机短信的功能,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • python dlib人脸识别代码实例

    python dlib人脸识别代码实例

    这篇文章主要介绍了python dlib人脸识别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Python3实现打印任意宽度的菱形代码

    Python3实现打印任意宽度的菱形代码

    这篇文章主要介绍了Python3实现打印任意宽度的菱形代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • tensorflow1.0学习之模型的保存与恢复(Saver)

    tensorflow1.0学习之模型的保存与恢复(Saver)

    这篇文章主要介绍了tensorflow1.0学习之模型的保存与恢复(Saver) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04

最新评论