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获取文件后缀名及批量更新目录下文件后缀名的方法
Nov 11 Python
Python的Django框架中settings文件的部署建议
May 30 Python
简单讲解Python中的闭包
Aug 11 Python
WINDOWS 同时安装 python2 python3 后 pip 错误的解决方法
Mar 16 Python
详解Python3操作Mongodb简明易懂教程
May 25 Python
使用django-crontab实现定时任务的示例
Feb 26 Python
python抓取京东小米8手机配置信息
Nov 13 Python
Python常见的pandas用法demo示例
Mar 16 Python
python的pyecharts绘制各种图表详细(附代码)
Nov 11 Python
python logging.basicConfig不生效的原因及解决
Feb 20 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
利用python实现逐步回归
Feb 24 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
浅析使用Turck-mmcache编译来加速、优化PHP代码
2013/06/20 PHP
php 批量替换html标签的实例代码
2013/11/26 PHP
详解Window7 下开发php扩展
2015/12/31 PHP
PHP使用socket发送HTTP请求的方法
2016/02/14 PHP
zen cart实现订单中增加paypal中预留电话的方法
2016/07/12 PHP
Yii 2中的load()和save()示例详解
2017/08/03 PHP
基于jquery实现状态限定编辑的代码
2012/02/11 Javascript
js正则表达式中test,exec,match方法的区别说明
2014/01/29 Javascript
CSS3,HTML5和jQuery搜索框集锦
2014/12/02 Javascript
原生JS封装Ajax插件(同域、jsonp跨域)
2016/05/03 Javascript
Angularjs 实现动态添加控件功能
2017/05/25 Javascript
Js实现京东无延迟菜单效果实例(demo)
2017/06/02 Javascript
JavaScript数组_动力节点Java学院整理
2017/06/26 Javascript
js断点调试心得分享(必看篇)
2017/12/08 Javascript
vue-test-utils初使用详解
2019/05/23 Javascript
javascript面向对象三大特征之继承实例详解
2019/07/24 Javascript
浅谈laytpl 模板空值显示null的解决方法及简单的js表达式
2019/09/19 Javascript
Vue使用路由钩子拦截器beforeEach和afterEach监听路由
2020/11/16 Javascript
[08:40]Navi Vs Newbee
2018/06/07 DOTA
Python编写屏幕截图程序方法
2015/02/18 Python
Python中防止sql注入的方法详解
2017/02/25 Python
AI人工智能 Python实现人机对话
2017/11/13 Python
python机器学习之决策树分类详解
2017/12/20 Python
python3.x实现发送邮件功能
2018/05/22 Python
Python中的Numpy矩阵操作
2018/08/12 Python
python 列表输出重复值以及对应的角标方法
2019/06/11 Python
python enumerate内置函数用法总结
2020/01/07 Python
谈谈python垃圾回收机制
2020/09/27 Python
css3学习心得分享
2013/08/19 HTML / CSS
迪拜领先运动补剂零售品牌中文站:Sporter商城
2019/08/20 全球购物
丝芙兰意大利官方网站:Sephora.it
2019/12/13 全球购物
信息工程学院毕业生推荐信
2013/11/05 职场文书
酒店管理专业毕业生自我鉴定
2014/09/29 职场文书
2015年乡镇科普工作总结
2015/05/13 职场文书
Android Flutter实现图片滑动切换效果
2022/04/07 Java/Android
win11系统中dhcp服务异常什么意思? Win11 DHCP服务异常修复方法
2022/04/08 数码科技