IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> pyqt5【designer】制作一个计算器 -> 正文阅读

[Python知识库]pyqt5【designer】制作一个计算器

支持重复运算

1.Ui_untitled.py文件

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/home/10318916@zte.intra/lxy/Python/study_one_day/untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(308, 341)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 150, 31, 26))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(90, 150, 31, 26))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(140, 150, 31, 26))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(Form)
        self.pushButton_4.setGeometry(QtCore.QRect(40, 190, 31, 26))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(Form)
        self.pushButton_5.setGeometry(QtCore.QRect(90, 190, 31, 26))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(Form)
        self.pushButton_6.setGeometry(QtCore.QRect(140, 190, 31, 26))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(Form)
        self.pushButton_7.setGeometry(QtCore.QRect(40, 230, 31, 26))
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_8 = QtWidgets.QPushButton(Form)
        self.pushButton_8.setGeometry(QtCore.QRect(90, 230, 31, 26))
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_9 = QtWidgets.QPushButton(Form)
        self.pushButton_9.setGeometry(QtCore.QRect(140, 230, 31, 26))
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_10 = QtWidgets.QPushButton(Form)
        self.pushButton_10.setGeometry(QtCore.QRect(40, 270, 51, 26))
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_11 = QtWidgets.QPushButton(Form)
        self.pushButton_11.setGeometry(QtCore.QRect(100, 270, 71, 26))
        self.pushButton_11.setObjectName("pushButton_11")
        self.pushButton_12 = QtWidgets.QPushButton(Form)
        self.pushButton_12.setGeometry(QtCore.QRect(190, 180, 71, 21))
        self.pushButton_12.setObjectName("pushButton_12")
        self.pushButton_13 = QtWidgets.QPushButton(Form)
        self.pushButton_13.setGeometry(QtCore.QRect(190, 210, 71, 21))
        self.pushButton_13.setObjectName("pushButton_13")
        self.pushButton_14 = QtWidgets.QPushButton(Form)
        self.pushButton_14.setGeometry(QtCore.QRect(190, 240, 71, 21))
        self.pushButton_14.setObjectName("pushButton_14")
        self.pushButton_15 = QtWidgets.QPushButton(Form)
        self.pushButton_15.setGeometry(QtCore.QRect(190, 270, 71, 21))
        self.pushButton_15.setObjectName("pushButton_15")
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(40, 20, 221, 121))
        self.textEdit.setObjectName("textEdit")
        self.pushButton_16 = QtWidgets.QPushButton(Form)
        self.pushButton_16.setGeometry(QtCore.QRect(190, 150, 71, 21))
        self.pushButton_16.setObjectName("pushButton_16")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "计算器"))
        self.pushButton.setText(_translate("Form", "1"))
        self.pushButton_2.setText(_translate("Form", "2"))
        self.pushButton_3.setText(_translate("Form", "3"))
        self.pushButton_4.setText(_translate("Form", "4"))
        self.pushButton_5.setText(_translate("Form", "5"))
        self.pushButton_6.setText(_translate("Form", "6"))
        self.pushButton_7.setText(_translate("Form", "7"))
        self.pushButton_8.setText(_translate("Form", "8"))
        self.pushButton_9.setText(_translate("Form", "9"))
        self.pushButton_10.setText(_translate("Form", "0"))
        self.pushButton_11.setText(_translate("Form", "="))
        self.pushButton_12.setText(_translate("Form", "+"))
        self.pushButton_13.setText(_translate("Form", "-"))
        self.pushButton_14.setText(_translate("Form", "*"))
        self.pushButton_15.setText(_translate("Form", "/"))
        self.pushButton_16.setText(_translate("Form", "AC"))

2.1.py文件

from Ui_untitled import *
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from functools import partial


g_now_data=0
g_last_data=0
symbol = '+'
display_string = ''

#####################################################
#                计算数据库组件
#####################################################
def sql_num_event(display_event_ui,num):
    global g_now_data
    global g_last_data
    global display_string
    display_string = display_string+str(num)
    g_now_data = g_now_data*10+int(num)

def sql_symbol_event(display_event_ui,sym):
    global symbol
    global g_now_data
    global g_last_data
    global display_string
    if str(sym)=='=':
        result = calculation_event(display_event_ui)
        display_string=display_string+'\n'+str(sym)
        display_string=display_string+str(result)
    else:
        g_last_data = calculation_event(display_event_ui)
        g_now_data = 0
        display_string=display_string+str(sym)
        symbol=str(sym)
#####################################################
#                核心计算组件
#####################################################
def calculation_event(display_event_ui):
    result = 0
    if symbol == '+':
        result = g_last_data + g_now_data
    elif symbol == '-':
        result = g_last_data - g_now_data
    elif symbol == '*':
        result = g_last_data * g_now_data
    elif symbol == '/':
        result = g_last_data / g_now_data
    return result
#####################################################
#                计算界面显示事件请求
#####################################################
def display_event(display_event_ui):
    display_event_ui.textEdit.setText(str(display_string))

def display_symbol_event(display_event_ui,sym):
    sql_symbol_event(display_event_ui,sym)
    display_event_ui.textEdit.setText(str(display_string))

def all_sql_display_event(display_event_ui,num):
    sql_num_event(display_event_ui,num)
    display_event(display_event_ui)

def all_AC_clear(display_event_ui,clear):
    global symbol
    global g_now_data
    global g_last_data
    global display_string
    symbol = '+'
    g_now_data = 0
    g_last_data = 0
    display_string = ''
    display_event_ui.textEdit.setText(str(display_string))
#####################################################
#                  按钮点击事件请求
#####################################################
def click_event_1(click_event_ui):
    all_sql_display_event(click_event_ui,1)
    return 1

def click_event_2(click_event_ui):
    all_sql_display_event(click_event_ui,2)
    return 2

def click_event_3(click_event_ui):
    all_sql_display_event(click_event_ui,3)
    return 3

def click_event_4(click_event_ui):
    all_sql_display_event(click_event_ui,4)
    return 4

def click_event_5(click_event_ui):
    all_sql_display_event(click_event_ui,5)
    return 5

def click_event_6(click_event_ui):
    all_sql_display_event(click_event_ui,6)
    return 6

def click_event_7(click_event_ui):
    all_sql_display_event(click_event_ui,7)
    return 7

def click_event_8(click_event_ui):
    all_sql_display_event(click_event_ui,8)
    return 8

def click_event_9(click_event_ui):
    all_sql_display_event(click_event_ui,9)
    return 9

def click_event_0(click_event_ui):
    all_sql_display_event(click_event_ui,0)
    return 0

def click_event_return(click_event_ui):
    display_symbol_event(click_event_ui,'=')   
    return '='

def click_event_add(click_event_ui):
    display_symbol_event(click_event_ui,'+')
    return '+'

def click_event_reduce(click_event_ui):
    display_symbol_event(click_event_ui,'-')
    return '-'

def click_event_ride(click_event_ui):
    display_symbol_event(click_event_ui,'*')
    return '*'

def click_event_division(click_event_ui):
    display_symbol_event(click_event_ui,'/')
    return '/'

def click_event_clear(click_event_ui):
    all_AC_clear(click_event_ui,'ac')
    return 'ac'
#####################################################
#                  按钮点击事件监控
#####################################################
def click_event_monitoring(click_ui):
    click_ui.pushButton.clicked.connect(partial(click_event_1,click_ui))
    click_ui.pushButton_2.clicked.connect(partial(click_event_2,click_ui))
    click_ui.pushButton_3.clicked.connect(partial(click_event_3,click_ui))
    click_ui.pushButton_4.clicked.connect(partial(click_event_4,click_ui))
    click_ui.pushButton_5.clicked.connect(partial(click_event_5,click_ui))
    click_ui.pushButton_6.clicked.connect(partial(click_event_6,click_ui))
    click_ui.pushButton_7.clicked.connect(partial(click_event_7,click_ui))
    click_ui.pushButton_8.clicked.connect(partial(click_event_8,click_ui))
    click_ui.pushButton_9.clicked.connect(partial(click_event_9,click_ui))

    click_ui.pushButton_10.clicked.connect(partial(click_event_0,click_ui))
    click_ui.pushButton_11.clicked.connect(partial(click_event_return,click_ui))
    click_ui.pushButton_12.clicked.connect(partial(click_event_add,click_ui))
    click_ui.pushButton_13.clicked.connect(partial(click_event_reduce,click_ui))
    click_ui.pushButton_14.clicked.connect(partial(click_event_ride,click_ui))
    click_ui.pushButton_15.clicked.connect(partial(click_event_division,click_ui))

    click_ui.pushButton_16.clicked.connect(partial(click_event_clear,click_ui))



#####################################################
#                  主函数
#####################################################
if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    w=Ui_Form()
    w.setupUi(MainWindow)


    
    click_event_monitoring(w)   #点击事件监控


    MainWindow.show()
    sys.exit(app.exec_())


    

3.效果展示

详细教程之后再做更新?

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-03-10 22:27:16  更:2022-03-10 22:29:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/29 20:13:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计