1.用Qt Designer创建界面,并转换为py文件。

2.创建一个main.py文件,编写逻辑代码。

在设计程序前,先要建立个项目文件,因为项目大了,里面会配置很多文件,比如资源文件,图片,ui文件,数据库文件,json文件,逻辑代码文件等。为了方便后期维护,开始设计时就要做一一定分类。这里做了个计算器项目。包含3个界面,分别为登陆界面,注册界面,计算器界面,目的主要是练习不同窗体之间的切换和交互。首先建立一个计算器文件,此文件里再创建个资源文件resource。再在resource创建imagehe 和ui文件用来存图片和ui文件。如下图:

注册ui文件转换代码

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'register.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Form(object):def setupUi(self, Form):Form.setObjectName("Form")Form.resize(500, 450)Form.setMinimumSize(QtCore.QSize(500, 450))Form.setMaximumSize(QtCore.QSize(500, 450))Form.setStyleSheet("QWidget#Form{\n"
"    \n"
"    border-image: url(:/image/view.jpg);\n"
"}")self.main_menu_btn = QtWidgets.QPushButton(Form)self.main_menu_btn.setGeometry(QtCore.QRect(10, 10, 50, 50))self.main_menu_btn.setStyleSheet("QPushButton {\n"
"    border-radius:25px;\n"
"    background-color:rgb(253,167,255);\n"
"    border:2px solid rgb(226,226,226);\n"
"    color:white;\n"
"    \n"
"}\n"
"QPushButton:hover {\n"
"    \n"
"    border:4px double rgb(239,160,179);\n"
"    \n"
"}\n"
"\n"
"QPushButton:checked {\n"
"    \n"
"    \n"
"    background-color: rgb(193, 6, 255);\n"
"    \n"
"}")self.main_menu_btn.setCheckable(True)self.main_menu_btn.setObjectName("main_menu_btn")self.main_about_btn = QtWidgets.QPushButton(Form)self.main_about_btn.setGeometry(QtCore.QRect(80, 10, 50, 50))self.main_about_btn.setStyleSheet("QPushButton {\n"
"    border-radius:25px;\n"
"    background-color:rgb(253,167,255);\n"
"    border:2px solid rgb(226,226,226);\n"
"    color:white;\n"
"    \n"
"}\n"
"QPushButton:hover {\n"
"    \n"
"    border:4px double rgb(239,160,179);\n"
"    \n"
"}\n"
"\n"
"QPushButton:checked {\n"
"    \n"
"    \n"
"    background-color: rgb(193, 6, 255);\n"
"    \n"
"}")self.main_about_btn.setCheckable(False)self.main_about_btn.setObjectName("main_about_btn")self.main_reset_btn = QtWidgets.QPushButton(Form)self.main_reset_btn.setGeometry(QtCore.QRect(60, 60, 50, 50))self.main_reset_btn.setStyleSheet("QPushButton {\n"
"    border-radius:25px;\n"
"    background-color:rgb(253,167,255);\n"
"    border:2px solid rgb(226,226,226);\n"
"    color:white;\n"
"    \n"
"}\n"
"QPushButton:hover {\n"
"    \n"
"    border:4px double rgb(239,160,179);\n"
"    \n"
"}\n"
"\n"
"QPushButton:checked {\n"
"    \n"
"    \n"
"    background-color: rgb(193, 6, 255);\n"
"    \n"
"}")self.main_reset_btn.setCheckable(False)self.main_reset_btn.setObjectName("main_reset_btn")self.main_exit_btn = QtWidgets.QPushButton(Form)self.main_exit_btn.setGeometry(QtCore.QRect(10, 90, 50, 50))self.main_exit_btn.setStyleSheet("QPushButton {\n"
"    border-radius:25px;\n"
"    background-color:rgb(253,167,255);\n"
"    border:2px solid rgb(226,226,226);\n"
"    color:white;\n"
"    \n"
"}\n"
"QPushButton:hover {\n"
"    \n"
"    border:4px double rgb(239,160,179);\n"
"    \n"
"}\n"
"\n"
"QPushButton:checked {\n"
"    \n"
"    \n"
"    background-color: rgb(193, 6, 255);\n"
"    \n"
"}")self.main_exit_btn.setCheckable(False)self.main_exit_btn.setObjectName("main_exit_btn")self.widget = QtWidgets.QWidget(Form)self.widget.setGeometry(QtCore.QRect(150, 140, 211, 201))self.widget.setObjectName("widget")self.formLayout = QtWidgets.QFormLayout(self.widget)self.formLayout.setContentsMargins(0, 0, 0, 0)self.formLayout.setVerticalSpacing(20)self.formLayout.setObjectName("formLayout")self.label = QtWidgets.QLabel(self.widget)self.label.setStyleSheet("\n"
"font: 14pt \"楷体\";\n"
"color: rgb(255, 0, 0);")self.label.setObjectName("label")self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)self.lineEdit = QtWidgets.QLineEdit(self.widget)self.lineEdit.setMinimumSize(QtCore.QSize(0, 30))self.lineEdit.setStyleSheet("background-color:transparent;\n"
"color: rgb(255, 0, 127);\n"
"font: 16pt \"宋体\";")self.lineEdit.setText("")self.lineEdit.setObjectName("lineEdit")self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)self.label_2 = QtWidgets.QLabel(self.widget)self.label_2.setStyleSheet("\n"
"font: 14pt \"楷体\";\n"
"color: rgb(255, 0, 0);")self.label_2.setObjectName("label_2")self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)self.lineEdit_2.setMinimumSize(QtCore.QSize(0, 30))self.lineEdit_2.setStyleSheet("background-color:transparent;\n"
"color: rgb(255, 0, 127);\n"
"font: 16pt \"宋体\";")self.lineEdit_2.setText("")self.lineEdit_2.setObjectName("lineEdit_2")self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)self.label_3 = QtWidgets.QLabel(self.widget)self.label_3.setStyleSheet("\n"
"font: 14pt \"楷体\";\n"
"color: rgb(255, 0, 0);")self.label_3.setObjectName("label_3")self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)self.lineEdit_3 = QtWidgets.QLineEdit(self.widget)self.lineEdit_3.setMinimumSize(QtCore.QSize(0, 30))self.lineEdit_3.setStyleSheet("background-color:transparent;\n"
"color: rgb(255, 0, 127);\n"
"font: 16pt \"宋体\";")self.lineEdit_3.setObjectName("lineEdit_3")self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_3)self.pushButton = QtWidgets.QPushButton(self.widget)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())self.pushButton.setSizePolicy(sizePolicy)self.pushButton.setMinimumSize(QtCore.QSize(0, 30))self.pushButton.setStyleSheet("QPushButton {\n"
"    \n"
"    background-color: rgb(255, 170, 127);\n"
"\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: rgb(255, 231, 215);\n"
"\n"
"}\n"
"QPushButton:pressed {\n"
"    \n"
"    background-color: rgb(168, 112, 83);\n"
"\n"
"}\n"
"")self.pushButton.setObjectName("pushButton")self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.pushButton)self.label.raise_()self.label_2.raise_()self.label_3.raise_()self.lineEdit.raise_()self.lineEdit_2.raise_()self.lineEdit_3.raise_()self.pushButton.raise_()self.main_about_btn.raise_()self.main_reset_btn.raise_()self.main_exit_btn.raise_()self.main_menu_btn.raise_()self.retranslateUi(Form)self.main_menu_btn.clicked['bool'].connect(Form.show_hide_menu) # type: ignoreself.main_about_btn.clicked.connect(Form.about_like) # type: ignoreself.main_reset_btn.clicked.connect(Form.reset_menu) # type: ignoreself.main_exit_btn.clicked.connect(Form.exit_pang) # type: ignoreself.pushButton.clicked.connect(Form.check_register) # type: ignoreQtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("Form", "Form"))self.main_menu_btn.setText(_translate("Form", "菜单"))self.main_about_btn.setText(_translate("Form", "关于"))self.main_reset_btn.setText(_translate("Form", "重置"))self.main_exit_btn.setText(_translate("Form", "退出"))self.label.setText(_translate("Form", "账   号:"))self.label_2.setText(_translate("Form", "密    码:"))self.label_3.setText(_translate("Form", "确认密码:"))self.pushButton.setText(_translate("Form", "注册"))
import register_rc

逻辑代码


from PyQt5.Qt import *
from 计算器实战项目.resource.register import Ui_Formclass Window(QWidget,Ui_Form):def __init__(self):super().__init__()self.setAttribute(Qt.WA_StyledBackground,True)self.setupUi(self)self.animation_targets=[self.main_about_btn,self.main_reset_btn,self.main_exit_btn]self.animation_targets_pos=[target.pos() for target in self.animation_targets]def show_hide_menu(self,checked):print(checked)if checked:animation_group = QSequentialAnimationGroup(self)for index,target in enumerate(self.animation_targets):anition=QPropertyAnimation()anition.setTargetObject(target)anition.setPropertyName(b"pos")anition.setStartValue(self.main_menu_btn.pos())anition.setEndValue(self.animation_targets_pos[index])anition.setDuration(100)anition.setEasingCurve(QEasingCurve.InOutBounce)animation_group.addAnimation(anition)animation_group.start(QAbstractAnimation.DeleteWhenStopped)else:animation_group = QSequentialAnimationGroup(self)for index, target in enumerate(self.animation_targets):anition = QPropertyAnimation()anition.setTargetObject(target)anition.setPropertyName(b"pos")anition.setEndValue(self.main_menu_btn.pos())anition.setStartValue(self.animation_targets_pos[index])anition.setDuration(100)anition.setEasingCurve(QEasingCurve.InOutBounce)animation_group.addAnimation(anition)animation_group.start(QAbstractAnimation.DeleteWhenStopped)def about_like(self):print("about")passdef reset_menu(self):print("reset")passdef exit_pang(self):print("exit")passdef check_register(self):print("check_reg")passif __name__ == '__main__':import sysapp=QApplication(sys.argv)win=Window()win.show()sys.exit(app.exec_())

上面动画组代码简化如下:

def show_hide_menu(self,checked):animation_group = QSequentialAnimationGroup(self)for index,target in enumerate(self.animation_targets):anition=QPropertyAnimation()anition.setTargetObject(target)anition.setPropertyName(b"pos")if checked:anition.setStartValue(self.main_menu_btn.pos())anition.setEndValue(self.animation_targets_pos[index])else:anition.setEndValue(self.main_menu_btn.pos())anition.setStartValue(self.animation_targets_pos[index])anition.setDuration(100)anition.setEasingCurve(QEasingCurve.InOutBounce)animation_group.addAnimation(anition)animation_group.start(QAbstractAnimation.DeleteWhenStopped)

利用动画组自带API参数设置动画状态

    def show_hide_menu(self,checked):animation_group = QSequentialAnimationGroup(self)for index,target in enumerate(self.animation_targets):anition=QPropertyAnimation()anition.setTargetObject(target)anition.setPropertyName(b"pos")anition.setStartValue(self.main_menu_btn.pos())anition.setEndValue(self.animation_targets_pos[index])anition.setDuration(100)anition.setEasingCurve(QEasingCurve.InOutBounce)animation_group.addAnimation(anition)if checked:animation_group.setDirection(QAbstractAnimation.Forward)else:animation_group.setDirection(QAbstractAnimation.Backward)animation_group.start(QAbstractAnimation.DeleteWhenStopped)

优化动画组代码

 def show_hide_menu(self,checked):animation_group = QSequentialAnimationGroup(self)for index,target in enumerate(self.animation_targets):anition=QPropertyAnimation()anition.setTargetObject(target)anition.setPropertyName(b"pos")anition.setStartValue(self.main_menu_btn.pos())anition.setEndValue(self.animation_targets_pos[index])anition.setDuration(100)anition.setEasingCurve(QEasingCurve.OutBounce)animation_group.addAnimation(anition)animation_group.setDirection(not checked)animation_group.start(QAbstractAnimation.DeleteWhenStopped)
#QAbstractAnimation.Forward 实际为枚举值,checked也是布尔值,可以把这两个给关联起来。上面代码就可以这样写了
'''
Backward = 1DeleteWhenStopped = 1Forward = 0KeepWhenStopped = 0Paused = 1Running = 2Stopped = 0
'''

pyqt5练习小项目相关推荐

  1. pyqt5优秀项目python_Python-PyQt5-第一个小项目--登录框--login

    Ps:水平有限,欢迎建议和挑错 我们在此之前,学习了PyQt的主窗口(QMainwindow),按钮(Qpushbutton),标签栏(QLabel),文本框编辑框(LineEdit)4个组件. 我们 ...

  2. 【Python • 项目实战】pytesseract+pyqt实现图片识别软件小项目——(一)创建项目和画界面

    本文默认你已经学习了上一篇文章,如果你还没有学习,就赶快去学习吧.关注博主,学习更多内容. 文章目录 前言 一.创建项目 1. 从模板创建项目 2. 使用pycharm打开项目 3. 安装Pyqt5库 ...

  3. python登陆界面代码_Python-PyQt5-第一个小项目--登录框--login

    Ps:水平有限,欢迎建议和挑错 我们在此之前,学习了PyQt的主窗口(QMainwindow),按钮(Qpushbutton),标签栏(QLabel),文本框编辑框(LineEdit)4个组件. 我们 ...

  4. Python实战小项目

    不是很稀饭<复联>嘛,看了<复联4>,就用50行Python代码做了这些: 视频展示:50行代码玩转<复仇者联盟> 教程地址:图片转字符画 相关教程地址:视频转字符 ...

  5. 仿qq左滑删除listview_Java基于Swing和Netty仿QQ界面聊天小项目

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:硬刚一周,3W字总结,一年的经验告诉你如何准备校招! 个人原创100W+访问量博客:点击前往,查看更多 来源:b ...

  6. 【敬初学者】Python基础学完了,该怎么知道自己学的怎么样呢?十个经典实战小项目附源码

    前言 1.街霸游戏 1.1 KO街霸 程序完整源码 程序的输出界面 1.2 春丽VS巴洛克 参考源码 2.猜谜游戏 2.1简单的猜数字游戏 项目要求 参考源码 2.2 进阶的猜姓名游戏 项目要求 参考 ...

  7. Java EE---使用Spring框架创建Market小项目

    通过Spring的IoC和DI思想创建一个小项目便于理解spring 题目: 1.使用Spring IOC/DI 模拟某超市管理功能,程序设计涉及: (1)Product类(商品类):含id(商品编号 ...

  8. Python练手小项目

    一.画爱心表白 1.图形都是由一系列的点(X,Y)构成的曲线,由于X,Y满足一定的关系,所以我们就可以建立模型,建立表达式expression,当满足时,两个for循环(for X in range: ...

  9. ios小项目——新浪微博客户端总结

    2019独角兽企业重金招聘Python工程师标准>>> 们还是登录不了,你们要用还是得自己申请appkey并且把回调网址设为baidu.或者是再下面留言,留下你的微博uid我把你加入 ...

最新文章

  1. vs怎么编译php文件,vscode怎么初始编译
  2. 回调函数案列(C高级)
  3. LL-verilog索引向量+-号用法
  4. mysql查询大于0的标记_MySQL如何查询回答数大于0的问题并分页
  5. nuxt2中使用less
  6. NOIP1996复赛 普及组 第二题 python
  7. ubuntu jdk tomcat mysql_Ubuntu下JDK+Tomcat+MySql环境的搭建
  8. WPF-005:关于使用PageFunction导航中KeepAlive的使用
  9. 别信了大佬的“鬼话”,想造无人驾驶到底进展如何吗?
  10. 如何判断python是否安装成功_python怎么判断模块安装完成
  11. 回顾:饶毅教授的一次采访
  12. activiti工作流 php,码云社 | 砺锋科技-SpringBoot整合Activiti工作流(附源码) - 用代码改变世界...
  13. 读Zepto源码之Touch模块
  14. 【技术向】如何借助Tushare,学习量化理财(入门)
  15. 谷歌play商店_如何从Google Play商店退款
  16. python制作动态表情包,用 Python 开发一个【GIF表情包制作神器】
  17. linux 内网ip扫描工具,C#开发:局域网IP地址扫描小工具
  18. 计算机上的字体太小怎么办,电脑字体太小怎么调 电脑字体调整方法有哪些
  19. 中国十六烷基磷酸钾行业市场供需与战略研究报告
  20. Java程序员未来职业应如何规划?

热门文章

  1. OSSIM开源安全信息管理系统(一)
  2. 优麒麟mysql_如何在优麒麟Ubuntu Kylin下安装Numix主题和图标
  3. Idea基本操作攻略
  4. 蓝牙地址解析(NAP/UAP/LAP)
  5. Mac下Sunny_Ngrok内网地址映射成外网
  6. 01_行销(Marketing)里的有用的KPI-转换率 (Conversion Rate)
  7. html5禁用右侧滚轮条,鼠标滚轮乱跳,教您鼠标滚轮乱跳怎么修复
  8. 鼠标上下滑轮时,来回乱跑的骚操作
  9. 2019大数据领域十大必读书籍
  10. 技术VC的优势以及技术VC是如何生存的