当前位置: 首页 > news >正文

沧州市高速公路建设管理局网站百度学术官网登录入口

沧州市高速公路建设管理局网站,百度学术官网登录入口,贵阳做网站做得好的,上海优刻得官网目录 一、环境配置: 二、代码实现 三、主程序 四、总结 本文使用PyQt5设计一款简单的计算器,可以通过界面交互实现加减乘除的功能,希望能够给初学者一些帮助。主要涉及的知识点有类的定义与初始化、类的成员函数、pyqt5的信号与槽函数等。…

目录

一、环境配置:

二、代码实现

三、主程序

四、总结


        本文使用PyQt5设计一款简单的计算器,可以通过界面交互实现加减乘除的功能,希望能够给初学者一些帮助。主要涉及的知识点有类的定义与初始化、类的成员函数、pyqt5的信号与槽函数等。

具体界面如下:

一、环境配置:

使用pip指令安装pyqt5,此处选择5.12.0版本,因为笔者安装的spyder版本为4.1.5,过高的版本不兼容:

pip install PyQt5==5.12.0 -i https://pypi.douban.com/simple
pip install PyQt5-tools -i https://pypi.douban.com/simple
pip install PyQt5designer -i https://pypi.douban.com/simple

二、代码实现

1、引入依赖库。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QLineEdit, QPushButton
from PyQt5.QtCore import Qt

2、定义计算器(Calculator)类,成员函数的功能如下表所示。

函数名称函数功能

__init__()

初始化函数,初始化窗口名称、尺寸、按钮名称、位置、信号响应函数等。

button_click(self, number)

选择数字与小数点

button_clear(self)

实现屏幕以及缓存清空的功能

button_add(self)

实现两个数相加的功能

button_subtract(self)

实现两个数相减的功能

button_multiply(self)

实现两个数相乘的功能

button_divide(self)

实现两个数相除的功能

button_equal(self)

首先判断执行的运算操作,调用对应的函数进行计算,并将结果显示在文本框中
class Calculator(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("计算器")self.setFixedSize(300, 350)   # 固定窗口大小central_widget = QWidget(self)self.setCentralWidget(central_widget)main_layout = QGridLayout()central_widget.setLayout(main_layout)# 添加文本框self.screen = QLineEdit()self.screen.setFixedHeight(40)self.screen.setAlignment(Qt.AlignRight)self.screen.setReadOnly(True)main_layout.addWidget(self.screen, 0, 0, 1, 4)# 添加按钮button_1 = QPushButton("1", clicked=lambda: self.button_click("1"))button_2 = QPushButton("2", clicked=lambda: self.button_click("2"))button_3 = QPushButton("3", clicked=lambda: self.button_click("3"))button_4 = QPushButton("4", clicked=lambda: self.button_click("4"))button_5 = QPushButton("5", clicked=lambda: self.button_click("5"))button_6 = QPushButton("6", clicked=lambda: self.button_click("6"))button_7 = QPushButton("7", clicked=lambda: self.button_click("7"))button_8 = QPushButton("8", clicked=lambda: self.button_click("8"))button_9 = QPushButton("9", clicked=lambda: self.button_click("9"))button_0 = QPushButton("0", clicked=lambda: self.button_click("0"))button_add = QPushButton("+", clicked=self.button_add)button_subtract = QPushButton("-", clicked=self.button_subtract)button_multiply = QPushButton("*", clicked=self.button_multiply)button_divide = QPushButton("/", clicked=self.button_divide)button_clear = QPushButton("清除", clicked=self.button_clear)button_equal = QPushButton("=", clicked=self.button_equal)main_layout.addWidget(button_7, 1, 0)main_layout.addWidget(button_8, 1, 1)main_layout.addWidget(button_9, 1, 2)main_layout.addWidget(button_divide, 1, 3)main_layout.addWidget(button_4, 2, 0)main_layout.addWidget(button_5, 2, 1)main_layout.addWidget(button_6, 2, 2)main_layout.addWidget(button_multiply, 2, 3)main_layout.addWidget(button_1, 3, 0)main_layout.addWidget(button_2, 3, 1)main_layout.addWidget(button_3, 3, 2)main_layout.addWidget(button_subtract, 3, 3)main_layout.addWidget(button_0, 4, 0)main_layout.addWidget(button_clear, 4, 1, 1, 2)main_layout.addWidget(button_add, 4, 3)main_layout.addWidget(button_equal, 5, 0, 1, 4)# 初始化变量self.first_num = Noneself.operation = Nonedef button_click(self, number):current = self.screen.text()self.screen.setText(current + number)def button_clear(self):self.screen.clear()self.first_num = Noneself.operation = Nonedef button_add(self):self.first_num = float(self.screen.text())self.screen.clear()self.operation = "add"def button_subtract(self):self.first_num = float(self.screen.text())self.screen.clear()self.operation = "subtract"def button_multiply(self):self.first_num = float(self.screen.text())self.screen.clear()self.operation = "multiply"def button_divide(self):self.first_num = float(self.screen.text())self.screen.clear()self.operation = "divide"def button_equal(self):second_num = float(self.screen.text())self.screen.clear()if self.operation == "add":result = self.first_num + second_numelif self.operation == "subtract":result = self.first_num - second_numelif self.operation == "multiply":result = self.first_num * second_numelif self.operation == "divide":if second_num == 0:result = "除数不能为 0"else:result = self.first_num / second_numself.screen.setText(str(result))

三、主程序

主程序首先创建窗口对象,接着创建计算器对象并显示窗口,最后设定终止条件。

if __name__ == "__main__":app = QApplication(sys.argv)calc = Calculator()calc.show()sys.exit(app.exec_())

四、总结

这个计算器的优点:

  • 采用了PyQt5模块和Python语言进行设计,实现较为简单,易于理解和修改。
  • 可以进行基本运算操作,包括加、减、乘、除等。

这个计算器的缺点:

  • 功能较为简单,只能进行基础的数学运算,无任何高级功能。
  • 输入输出仅支持数字和基本的加减乘除运算符号,不支持其他字符、函数或变量。
  • 在除数为0时无法做出错误提示,故看起来好像可以除以0一样。

综上所述,该计算器适合作为一个小型的实验项目或界面设计入门示例,但是并不够强大,不能满足更多复杂应用场景的需求。

http://www.ds6.com.cn/news/12508.html

相关文章:

  • 中国电信 网站备案win7系统优化大师
  • dogip网站开发金华网站建设
  • 程序员培训班要什么学历无锡百度快照优化排名
  • 郑州同济医院靠谱吗广州优化防控措施
  • 龙口网站制作公司软件推广赚佣金渠道
  • 怎么才能建立网站实时seo排名点击软件
  • 四川建设网和四川省公共资源交易信息网安卓优化大师官方下载
  • 公司网站被黑有不良信息 做笔录广告联盟平台挂机赚钱
  • 小程序网站开发公司域名查询 ip
  • 做视频解析网站香港头条新闻
  • 西安高端网站建设首选百度竞价开户3000
  • 手机网易网自动app优化最新版
  • 扫描购物网站建设北京高端网站建设
  • 安阳做网站的费用seo网站关键词优化怎么做
  • 廊坊哪里有做网站的如何自己制作网站
  • win2008 建立网站关键词查询工具
  • 网站做轮播图的意义图片识别搜索引擎
  • dw旅游网站模板下载百度官网首页下载
  • 大庆今天最新公告北京seo优化多少钱
  • 做数学题挣钱的网站seo软件优化工具软件
  • 做投融资平台的网站都有哪些?手机广告推广软件
  • 做淘宝优惠券网站要多少钱互联网营销师考证多少钱
  • 做外贸家纺资料网站windows优化大师卸载
  • 关于网站建设外文文献百度搜索推广方法
  • 邢台网站建设最新报价seo整站优化方案
  • web2py做的网站免费软文发布平台有哪些
  • 在香港做网站需要什么企业qq和个人qq有什么区别
  • 网站规划的流程个人博客模板
  • 网站源码下载软件营销网站建设哪家好
  • Wordpress网站调用代码做网站设计的公司