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中的元组概念
May 21 Python
python+selenium实现163邮箱自动登陆的方法
Dec 31 Python
Python实现注册、登录小程序功能
Sep 21 Python
python占位符输入方式实例
May 27 Python
python字符串查找函数的用法详解
Jul 08 Python
Python中的 sort 和 sorted的用法与区别
Aug 10 Python
Python Tkinter模块 GUI 可视化实例
Nov 20 Python
python中的split()函数和os.path.split()函数使用详解
Dec 21 Python
在django中使用apscheduler 执行计划任务的实现方法
Feb 11 Python
python pandas利用fillna方法实现部分自动填充功能
Mar 16 Python
Python常用数字处理基本操作汇总
Sep 10 Python
Python用requests库爬取返回为空的解决办法
Feb 21 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
zend framework多模块多布局配置
2011/02/26 PHP
PHP下载大文件失败并限制下载速度的实例代码
2019/05/10 PHP
laravel 解决多库下的DB::transaction()事务失效问题
2019/10/21 PHP
javascript innerHTML、outerHTML、innerText、outerText的区别
2008/11/24 Javascript
artDialog双击会关闭对话框的修改过程分享
2013/08/05 Javascript
js通过八个点 拖动改变div大小的实现方法
2014/03/05 Javascript
js和jquery如何获取图片真实的宽度和高度
2014/09/28 Javascript
JavaScript控制图片加载完成后调用回调函数的方法
2015/03/20 Javascript
jquery 动态合并单元格的实现方法
2016/08/26 Javascript
jQuery事件对象总结
2016/10/17 Javascript
详解AngularJS验证、过滤器、指令
2017/01/04 Javascript
JS检测是否可以访问公网服务器功能代码
2017/06/19 Javascript
JS点击动态添加标签、删除指定标签的代码
2018/04/18 Javascript
jquery选择器和属性对象的操作实例分析
2020/01/10 jQuery
在Vue里如何把网页的数据导出到Excel的方法
2020/09/30 Javascript
Python中针对函数处理的特殊方法
2014/03/06 Python
Python解惑之True和False详解
2017/04/24 Python
python如何为被装饰的函数保留元数据
2018/03/21 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
2019/08/30 Python
Python脚本实现监听服务器的思路代码详解
2020/05/28 Python
python 元组的使用方法
2020/06/09 Python
Python图像识别+KNN求解数独的实现
2020/11/13 Python
Python 微信公众号文章爬取的示例代码
2020/11/30 Python
详解基于Facecognition+Opencv快速搭建人脸识别及跟踪应用
2021/01/21 Python
英国玛莎百货澳大利亚:Marks & Spencer Australia
2019/08/30 全球购物
remote接口和home接口主要作用
2013/05/15 面试题
一套中级Java程序员笔试题
2015/01/14 面试题
后勤人员岗位职责
2013/12/17 职场文书
境外导游求职信
2014/02/27 职场文书
房屋租赁意向书
2014/04/01 职场文书
单位在职证明书
2014/09/11 职场文书
新学期红领巾广播稿
2014/10/04 职场文书
2014年挂职干部工作总结
2014/12/06 职场文书
k8s部署redis cluster集群的实现
2021/06/24 Redis
Vue的过滤器你真了解吗
2022/02/24 Vue.js
Mysql 文件配置解析介绍
2022/05/06 MySQL