当前位置:脚本大全 > > 正文

pyqt5实例讲解(PyQt5实现简易计算器)

时间:2021-11-04 19:51:02类别:脚本大全

pyqt5实例讲解

PyQt5实现简易计算器

本文实例为大家分享了pyqt5实现简易计算器的具体代码,供大家参考,具体内容如下

效果图:

pyqt5实例讲解(PyQt5实现简易计算器)

界面代码

calc_interface.py

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • # -*- coding: utf-8 -*-
  •  
  • # form implementation generated from reading ui file 'calc_interface.ui'
  • #
  • # created by: pyqt5 ui code generator 5.8.2
  • #
  • # warning! all changes made in this file will be lost!
  •  
  • from pyqt5 import qtcore, qtgui, qtwidgets
  •  
  • class ui_mainwindow(object):
  •  def setupui(self, mainwindow):
  •   mainwindow.setobjectname("mainwindow")
  •   mainwindow.resize(647, 675)
  •   self.centralwidget = qtwidgets.qwidget(mainwindow)
  •   self.centralwidget.setobjectname("centralwidget")
  •   self.b_1 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_1.setgeometry(qtcore.qrect(10, 330, 71, 71))
  •   self.b_1.setobjectname("b_1")
  •   self.b_2 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_2.setgeometry(qtcore.qrect(80, 330, 71, 71))
  •   self.b_2.setobjectname("b_2")
  •   self.b_3 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_3.setgeometry(qtcore.qrect(150, 330, 71, 71))
  •   self.b_3.setobjectname("b_3")
  •   self.b_6 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_6.setgeometry(qtcore.qrect(150, 400, 71, 71))
  •   self.b_6.setobjectname("b_6")
  •   self.b_4 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_4.setgeometry(qtcore.qrect(10, 400, 71, 71))
  •   self.b_4.setobjectname("b_4")
  •   self.b_5 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_5.setgeometry(qtcore.qrect(80, 400, 71, 71))
  •   self.b_5.setobjectname("b_5")
  •   self.b_8 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_8.setgeometry(qtcore.qrect(80, 470, 71, 71))
  •   self.b_8.setobjectname("b_8")
  •   self.b_9 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_9.setgeometry(qtcore.qrect(150, 470, 71, 71))
  •   self.b_9.setobjectname("b_9")
  •   self.b_7 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_7.setgeometry(qtcore.qrect(10, 470, 71, 71))
  •   self.b_7.setobjectname("b_7")
  •   self.b_eq = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_eq.setgeometry(qtcore.qrect(150, 540, 211, 71))
  •   self.b_eq.setobjectname("b_eq")
  •   self.b_mod = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_mod.setgeometry(qtcore.qrect(220, 400, 71, 71))
  •   self.b_mod.setobjectname("b_mod")
  •   self.b_bra_l = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_bra_l.setgeometry(qtcore.qrect(220, 330, 71, 71))
  •   self.b_bra_l.setobjectname("b_bra_l")
  •   self.b_li = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_li.setgeometry(qtcore.qrect(220, 260, 71, 71))
  •   self.b_li.setobjectname("b_li")
  •   self.b_add = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_add.setgeometry(qtcore.qrect(10, 260, 71, 71))
  •   self.b_add.setobjectname("b_add")
  •   self.b_sub = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_sub.setgeometry(qtcore.qrect(80, 260, 71, 71))
  •   self.b_sub.setobjectname("b_sub")
  •   self.b_mul = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_mul.setgeometry(qtcore.qrect(150, 260, 71, 71))
  •   self.b_mul.setobjectname("b_mul")
  •   self.b_pow = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_pow.setgeometry(qtcore.qrect(290, 260, 71, 71))
  •   self.b_pow.setobjectname("b_pow")
  •   self.b_pai = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_pai.setgeometry(qtcore.qrect(290, 400, 71, 71))
  •   self.b_pai.setobjectname("b_pai")
  •   self.b_bra_r = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_bra_r.setgeometry(qtcore.qrect(290, 330, 71, 71))
  •   self.b_bra_r.setobjectname("b_bra_r")
  •   self.l_hist = qtwidgets.qlistwidget(self.centralwidget)
  •   self.l_hist.setgeometry(qtcore.qrect(380, 10, 256, 601))
  •   self.l_hist.setobjectname("l_hist")
  •   self.e_view = qtwidgets.qtextedit(self.centralwidget)
  •   self.e_view.setgeometry(qtcore.qrect(10, 10, 351, 231))
  •   font = qtgui.qfont()
  •   font.setfamily("adobe caslon pro bold")
  •   font.setpointsize(22)
  •   font.setbold(true)
  •   font.setweight(75)
  •   self.e_view.setfont(font)
  •   self.e_view.setobjectname("e_view")
  •   self.b_0 = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_0.setgeometry(qtcore.qrect(80, 540, 71, 71))
  •   self.b_0.setobjectname("b_0")
  •   self.b_pt = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_pt.setgeometry(qtcore.qrect(10, 540, 71, 71))
  •   self.b_pt.setobjectname("b_pt")
  •   self.b_del = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_del.setgeometry(qtcore.qrect(220, 470, 71, 71))
  •   self.b_del.setobjectname("b_del")
  •   self.b_clc = qtwidgets.qpushbutton(self.centralwidget)
  •   self.b_clc.setgeometry(qtcore.qrect(290, 470, 71, 71))
  •   self.b_clc.setobjectname("b_clc")
  •   mainwindow.setcentralwidget(self.centralwidget)
  •   self.menubar = qtwidgets.qmenubar(mainwindow)
  •   self.menubar.setgeometry(qtcore.qrect(0, 0, 647, 30))
  •   self.menubar.setobjectname("menubar")
  •   mainwindow.setmenubar(self.menubar)
  •   self.statusbar = qtwidgets.qstatusbar(mainwindow)
  •   self.statusbar.setobjectname("statusbar")
  •   mainwindow.setstatusbar(self.statusbar)
  •  
  •   self.retranslateui(mainwindow)
  •   qtcore.qmetaobject.connectslotsbyname(mainwindow)
  •  
  •  def retranslateui(self, mainwindow):
  •   _translate = qtcore.qcoreapplication.translate
  •   mainwindow.setwindowtitle(_translate("mainwindow", "mainwindow"))
  •   self.b_1.settext(_translate("mainwindow", "1"))
  •   self.b_2.settext(_translate("mainwindow", "2"))
  •   self.b_3.settext(_translate("mainwindow", "3"))
  •   self.b_6.settext(_translate("mainwindow", "6"))
  •   self.b_4.settext(_translate("mainwindow", "4"))
  •   self.b_5.settext(_translate("mainwindow", "5"))
  •   self.b_8.settext(_translate("mainwindow", "8"))
  •   self.b_9.settext(_translate("mainwindow", "9"))
  •   self.b_7.settext(_translate("mainwindow", "7"))
  •   self.b_eq.settext(_translate("mainwindow", "="))
  •   self.b_mod.settext(_translate("mainwindow", "%"))
  •   self.b_bra_l.settext(_translate("mainwindow", "("))
  •   self.b_li.settext(_translate("mainwindow", "/"))
  •   self.b_add.settext(_translate("mainwindow", "+"))
  •   self.b_sub.settext(_translate("mainwindow", "-"))
  •   self.b_mul.settext(_translate("mainwindow", "*"))
  •   self.b_pow.settext(_translate("mainwindow", "^"))
  •   self.b_pai.settext(_translate("mainwindow", "π"))
  •   self.b_bra_r.settext(_translate("mainwindow", ")"))
  •   self.b_0.settext(_translate("mainwindow", "0"))
  •   self.b_pt.settext(_translate("mainwindow", "."))
  •   self.b_del.settext(_translate("mainwindow", "删除"))
  •   self.b_clc.settext(_translate("mainwindow", "清空"))
  • 主逻辑代码

    calc_main.py

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • from pyqt5.qtcore import *
  • from pyqt5.qtgui import *
  • from pyqt5.qtwidgets import *
  • from calc_interface import ui_mainwindow
  • import os,sys
  •  
  • global e_view
  •  
  • pluginspath='pyqt5/qt/plugins'
  • if os.path.exists(pluginspath):#指定插件路径。源码运行时不会生效,打包后运行检测到路径,加载插件
  •  qapplication.addlibrarypath(pluginspath)
  •  
  • class mymainwindow(qmainwindow, ui_mainwindow):
  •  
  •  def forge_link(self):
  •   self.b_0.clicked.connect(self.button_event(0))
  •   self.b_1.clicked.connect(self.button_event(1))
  •   self.b_2.clicked.connect(self.button_event(2))
  • 标签:

    猜您喜欢