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


Posted in Python onMay 13, 2020

一、API接口设置

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

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

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

1.1 常用的一些设置

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

参数 作用
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自动补全

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

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判断Abundant Number的方法
Jun 15 Python
简单解析Django框架中的表单验证
Jul 17 Python
Python爬取网页中的图片(搜狗图片)详解
Mar 23 Python
简单的python协同过滤程序实例代码
Jan 31 Python
Python使用pydub库对mp3与wav格式进行互转的方法
Jan 10 Python
python 通过SSHTunnelForwarder隧道连接redis的方法
Feb 19 Python
基于django channel实现websocket的聊天室的方法示例
Apr 11 Python
python增加图像对比度的方法
Jul 12 Python
pytorch中的自定义数据处理详解
Jan 06 Python
pycharm设置当前工作目录的操作(working directory)
Feb 14 Python
如何基于python实现不邻接植花
May 01 Python
浅谈TensorFlow之稀疏张量表示
Jun 30 Python
Python tkinter实现简单加法计算器代码实例
May 13 #Python
Django权限设置及验证方式
May 13 #Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
May 13 #Python
django自带的权限管理Permission用法说明
May 13 #Python
Python基于jieba, wordcloud库生成中文词云
May 13 #Python
django admin 根据choice字段选择的不同来显示不同的页面方式
May 13 #Python
Jupyter notebook如何实现指定浏览器打开
May 13 #Python
You might like
php中echo()和print()、require()和include()等易混淆函数的区别
2012/02/22 PHP
phpmailer中文乱码问题的解决方法
2014/04/22 PHP
Centos PHP 扩展Xchche的安装教程
2016/07/09 PHP
PHP getNamespaces()函数讲解
2019/02/03 PHP
jquery插件 autoComboBox 下拉框
2010/12/22 Javascript
JavaScript prototype属性深入介绍
2012/11/27 Javascript
JQuery设置和去除disabled属性的5种方法总结
2013/05/16 Javascript
javascript-简单的计算器实现步骤分解(附图)
2013/05/30 Javascript
xmlhttp缓存清除的2种解决方法
2013/12/13 Javascript
在Firefox下js select标签点击无法弹出
2014/03/06 Javascript
Jquery validation remote 验证的缓存问题解决方法
2014/03/25 Javascript
纯js实现div内图片自适应大小(已测试,兼容火狐)
2014/06/16 Javascript
一个不错的js html页面倒计时可精确到秒
2014/10/22 Javascript
常用的jQuery前端技巧收集
2014/12/24 Javascript
JS实现跟随鼠标闪烁转动色块的方法
2015/02/26 Javascript
微信小程序 教程之模板
2016/10/18 Javascript
CKEditor扩展插件:自动排版功能autoformat插件实现方法详解
2020/02/06 Javascript
JS数组降维的实现Array.prototype.concat.apply([], arr)
2020/04/28 Javascript
[03:43]TI9战队采访——PSG.LGD
2019/08/22 DOTA
Python实现的括号匹配判断功能示例
2018/08/25 Python
Django框架使用mysql视图操作示例
2019/05/15 Python
搭建python django虚拟环境完整步骤详解
2019/07/08 Python
python之拟合的实现
2019/07/19 Python
浅谈Python_Openpyxl使用(最全总结)
2019/09/05 Python
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
2020/04/15 Python
python+selenium+chrome批量文件下载并自动创建文件夹实例
2020/04/27 Python
Python中openpyxl实现vlookup函数的实例
2020/10/28 Python
python中str内置函数用法总结
2020/12/27 Python
函授大专自我鉴定
2013/11/01 职场文书
医学生职业规划范文
2014/01/05 职场文书
信息专业学生学习的自我评价
2014/02/17 职场文书
《少年王冕》教学反思
2014/04/11 职场文书
个人承诺书格式
2014/06/03 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
教你用eclipse连接mysql数据库
2021/04/22 MySQL
基于PostgreSQL/openGauss 的分布式数据库解决方案
2021/12/06 PostgreSQL