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 相关文章推荐
wxpython 学习笔记 第一天
Mar 16 Python
Python中文件I/O高效操作处理的技巧分享
Feb 04 Python
Python常见加密模块用法分析【MD5,sha,crypt模块】
May 24 Python
Python实现K折交叉验证法的方法步骤
Jul 11 Python
python使用HTMLTestRunner导出饼图分析报告的方法
Dec 30 Python
Tensorflow限制CPU个数实例
Feb 06 Python
tensorflow的ckpt及pb模型持久化方式及转化详解
Feb 12 Python
python扫描线填充算法详解
Feb 19 Python
Python图像处理库PIL的ImageDraw模块介绍详解
Feb 26 Python
PyCharm 2020.2.2 x64 下载并安装的详细教程
Oct 15 Python
Python中使用Selenium环境安装的方法步骤
Feb 22 Python
两行代码解决Jupyter Notebook中文不能显示的问题
Apr 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
php广告加载类用法实例
2014/09/23 PHP
PHP+shell脚本操作Memcached和Apache Status的实例分享
2016/03/11 PHP
laravel清除视图缓存的代码
2019/10/23 PHP
javascript json 新手入门文档
2009/12/03 Javascript
简单的Jquery遮罩层代码实例
2013/11/14 Javascript
JavaScript返回当前会话cookie全部键值对照的方法
2015/04/03 Javascript
jQuery图片旋转插件jQueryRotate.js用法实例(附demo下载)
2016/01/21 Javascript
BootStrap栅格系统、表单样式与按钮样式源码解析
2017/01/20 Javascript
利用types增强vscode中js代码提示功能详解
2017/07/07 Javascript
layer子层给父层页面元素赋值,以达到向父层页面传值的效果实例
2017/09/22 Javascript
Vue安装浏览器开发工具的步骤详解
2019/05/12 Javascript
vue实现搜索功能
2019/05/28 Javascript
JAVA面试题 static关键字详解
2019/07/16 Javascript
JS随机密码生成算法
2019/09/23 Javascript
js使用文档就绪函数动态改变页面内容示例【innerHTML、innerText】
2019/11/07 Javascript
浅谈vue 多个变量同时赋相同值互相影响
2020/08/05 Javascript
ES6字符串的扩展实例
2020/12/21 Javascript
Python 的类、继承和多态详解
2017/07/16 Python
python pandas读取csv后,获取列标签的方法
2018/11/12 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
2019/04/12 Python
Python使用selenium + headless chrome获取网页内容的方法示例
2019/10/16 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
2020/03/08 Python
keras .h5转移动端的.tflite文件实现方式
2020/05/25 Python
基于matplotlib中ion()和ioff()的使用详解
2020/06/16 Python
Python计算矩阵的和积的实例详解
2020/09/10 Python
HTML5 Canvas绘制文本及图片的基础教程
2016/03/14 HTML / CSS
美国性感女装网站:bebe
2017/03/04 全球购物
企划经理的岗位职责
2013/11/17 职场文书
11月升旗仪式讲话稿
2014/02/15 职场文书
公司总经理岗位职责
2014/03/15 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
商家认证委托书格式
2014/10/16 职场文书
画展邀请函
2015/01/31 职场文书
2015年七一建党节慰问信
2015/03/23 职场文书
Python中的程序流程控制语句
2022/02/24 Python
了解MySQL查询语句执行过程(5大组件)
2022/08/14 MySQL