今天就跟大家聊聊有關(guān)利用python怎么編寫一個(gè)計(jì)算器功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)一直在為企業(yè)提供服務(wù),多年的磨煉,使我們?cè)趧?chuàng)意設(shè)計(jì),營(yíng)銷型網(wǎng)站建設(shè)到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗(yàn)。我們擅長(zhǎng)傾聽企業(yè)需求,挖掘用戶對(duì)產(chǎn)品需求服務(wù)價(jià)值,為企業(yè)制作有用的創(chuàng)意設(shè)計(jì)體驗(yàn)。核心團(tuán)隊(duì)擁有超過(guò)10多年以上行業(yè)經(jīng)驗(yàn),涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)棕樹數(shù)據(jù)中心、成都APP應(yīng)用開發(fā)、手機(jī)移動(dòng)建站、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)絡(luò)整合營(yíng)銷。Python計(jì)算器加減乘除,供大家參考,具體內(nèi)容如下
1、效果圖
2、代碼
# coding=utf-8 import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QLCDNumber, QSlider, QVBoxLayout, qApp, \ QMainWindow from PyQt5.QtCore import Qt class ForExample(QWidget): def __init__(self): super().__init__() # self.clu = 0.0 # print('lx',type(self.clu)) self.info = 0.0 print(type(self.info)) self.statusSUN = False self.statusPoint = False self.initUI() def initUI(self): # 網(wǎng)格布局 grid = QGridLayout() self.setLayout(grid) # names = ['清除', '后退', '', '關(guān)閉', # '7', '8', '9', '/', # '4', '5', '6', '*', # '1', '2', '3', '-', # '0', '.', '=', '+'] # 創(chuàng)建一個(gè)菜單欄 # menubar = self.menuBar() # # 添加菜單 # fileMenu = menubar.addMenu('&File') # LCD顯示屏 self.lcd = QLCDNumber(self) self.lcd.setDigitCount(9) self.lcd.isRightToLeft() self.lcd.display(self.info) grid.addWidget(self.lcd, 0, 0, 2, 4) # 清除 bc = QPushButton('清除') bc.clicked.connect(self.def_bc) grid.addWidget(bc, 2, 0) # 后退 bk = QPushButton('后退') bk.clicked.connect(self.def_bk) grid.addWidget(bk, 2, 1) # ca = QPushButton('計(jì)算') # ca.clicked.connect(self.def_bc) # grid.addWidget(ca, 2, 2) of = QPushButton('關(guān)閉') of.clicked.connect(qApp.quit) grid.addWidget(of, 2, 3) # b7 = QPushButton('7') b7.clicked.connect(self.def_b7) grid.addWidget(b7, 3, 0) b8 = QPushButton('8') b8.clicked.connect(self.def_b8) grid.addWidget(b8, 3, 1) b9 = QPushButton('9') b9.clicked.connect(self.def_b9) grid.addWidget(b9, 3, 2) b4 = QPushButton('4') b4.clicked.connect(self.def_b4) grid.addWidget(b4, 4, 0) b5 = QPushButton('5') b5.clicked.connect(self.def_b5) grid.addWidget(b5, 4, 1) b6 = QPushButton('6') b6.clicked.connect(self.def_b6) grid.addWidget(b6, 4, 2) bExcept = QPushButton('/') bExcept.clicked.connect(self.def_bExcept) grid.addWidget(bExcept, 3, 3) bMultiply = QPushButton('*') bMultiply.clicked.connect(self.def_bMultiply) grid.addWidget(bMultiply, 4, 3) b1 = QPushButton('1') b1.clicked.connect(self.def_b1) grid.addWidget(b1, 5, 0) b2 = QPushButton('2') b2.clicked.connect(self.def_b2) grid.addWidget(b2, 5, 1) b3 = QPushButton('3') b3.clicked.connect(self.def_b3) grid.addWidget(b3, 5, 2) bLess = QPushButton('-') bLess.clicked.connect(self.def_bLess) grid.addWidget(bLess, 5, 3) b0 = QPushButton('0') b0.clicked.connect(self.def_b0) grid.addWidget(b0, 6, 0) bPoint = QPushButton('.') bPoint.clicked.connect(self.def_bPoint) grid.addWidget(bPoint, 6, 1) bsun = QPushButton('+') bsun.clicked.connect(self.def_bsun) grid.addWidget(bsun, 6, 3) bsun = QPushButton('=') bsun.clicked.connect(self.def_bresult) grid.addWidget(bsun, 6, 2) self.setGeometry(300, 300, 250, 250) self.show() ############################################################# def def_bc(self): self.info = 0 self.clu = 0 self.statusSUN = False self.lcd.display(self.info) def def_bk(self): if len(str(self.info)) > 1: self.info = int(str(self.info)[:-1]) else: self.info = 0 self.lcd.display(self.info) def def_b7(self): if self.info == 0: self.info = 7 else: self.info = int(str(self.info) + '7') self.statusSUN = False self.lcd.display(self.info) def def_b8(self): if self.info == 0: self.info = 8 else: self.info = int(str(self.info) + '8') self.statusSUN = False self.lcd.display(self.info) def def_b9(self): if self.info == 0: self.info = 9 else: self.info = int(str(self.info) + '9') self.statusSUN = False self.lcd.display(self.info) # 除 def def_bExcept(self): pass def def_b4(self): if self.info == 0: self.info = 4 else: self.info = int(str(self.info) + '4') self.statusSUN = False self.lcd.display(self.info) def def_b5(self): if self.info == 0: self.info = 5 else: self.info = int(str(self.info) + '5') self.statusSUN = False self.lcd.display(self.info) def def_b6(self): if self.info == 0: self.info = 6 else: self.info = int(str(self.info) + '6') self.statusSUN = False self.lcd.display(self.info) # 乘 def def_bMultiply(self): pass def def_b1(self): if self.info == 0: self.info = 1 else: self.info = int(str(self.info) + '1') self.statusSUN = False self.lcd.display(self.info) def def_b2(self): if self.info == 0: self.info = 2 else: self.info = int(str(self.info) + '2') self.statusSUN = False self.lcd.display(self.info) def def_b3(self): if self.info == 0: self.info = 3 else: self.info = int(str(self.info) + '3') self.statusSUN = False self.lcd.display(self.info) # 減 def def_bLess(self): pass def def_b0(self): if self.info == 0: self.info = 0 else: self.info = int(str(self.info) + '0') self.statusSUN = False self.lcd.display(self.info) def def_bPoint(self): if self.statusPoint: pass else: self.statusPoint = True if self.info == 0: self.info = 0. else: self.info = int(str(self.info) + '.') self.statusSUN = False self.lcd.display(self.info) # 等于,根據(jù)self.status判斷輸入的計(jì)算符號(hào) def def_bresult(self): self.clu += self.info self.lcd.display(self.clu) self.statusSUN = False self.info = 0 # 加 def def_bsun(self): self.clu += self.info self.lcd.display(self.clu) self.statusSUN = True self.info = 0 if __name__ == '__main__': app = QApplication(sys.argv) fe = ForExample() sys.exit(app.exec_()) # coding=utf-8 import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QLCDNumber, QSlider, QVBoxLayout, qApp, \ QMainWindow from PyQt5.QtCore import Qt class ForExample(QWidget): def __init__(self): super().__init__() # self.clu = 0.0 # print('lx',type(self.clu)) self.info = 0.0 print(type(self.info)) self.statusSUN = False self.statusPoint = False self.initUI() def initUI(self): # 網(wǎng)格布局 grid = QGridLayout() self.setLayout(grid) # names = ['清除', '后退', '', '關(guān)閉', # '7', '8', '9', '/', # '4', '5', '6', '*', # '1', '2', '3', '-', # '0', '.', '=', '+'] # 創(chuàng)建一個(gè)菜單欄 # menubar = self.menuBar() # # 添加菜單 # fileMenu = menubar.addMenu('&File') # LCD顯示屏 self.lcd = QLCDNumber(self) self.lcd.setDigitCount(9) self.lcd.isRightToLeft() self.lcd.display(self.info) grid.addWidget(self.lcd, 0, 0, 2, 4) # 清除 bc = QPushButton('清除') bc.clicked.connect(self.def_bc) grid.addWidget(bc, 2, 0) # 后退 bk = QPushButton('后退') bk.clicked.connect(self.def_bk) grid.addWidget(bk, 2, 1) # ca = QPushButton('計(jì)算') # ca.clicked.connect(self.def_bc) # grid.addWidget(ca, 2, 2) of = QPushButton('關(guān)閉') of.clicked.connect(qApp.quit) grid.addWidget(of, 2, 3) # b7 = QPushButton('7') b7.clicked.connect(self.def_b7) grid.addWidget(b7, 3, 0) b8 = QPushButton('8') b8.clicked.connect(self.def_b8) grid.addWidget(b8, 3, 1) b9 = QPushButton('9') b9.clicked.connect(self.def_b9) grid.addWidget(b9, 3, 2) b4 = QPushButton('4') b4.clicked.connect(self.def_b4) grid.addWidget(b4, 4, 0) b5 = QPushButton('5') b5.clicked.connect(self.def_b5) grid.addWidget(b5, 4, 1) b6 = QPushButton('6') b6.clicked.connect(self.def_b6) grid.addWidget(b6, 4, 2) bExcept = QPushButton('/') bExcept.clicked.connect(self.def_bExcept) grid.addWidget(bExcept, 3, 3) bMultiply = QPushButton('*') bMultiply.clicked.connect(self.def_bMultiply) grid.addWidget(bMultiply, 4, 3) b1 = QPushButton('1') b1.clicked.connect(self.def_b1) grid.addWidget(b1, 5, 0) b2 = QPushButton('2') b2.clicked.connect(self.def_b2) grid.addWidget(b2, 5, 1) b3 = QPushButton('3') b3.clicked.connect(self.def_b3) grid.addWidget(b3, 5, 2) bLess = QPushButton('-') bLess.clicked.connect(self.def_bLess) grid.addWidget(bLess, 5, 3) b0 = QPushButton('0') b0.clicked.connect(self.def_b0) grid.addWidget(b0, 6, 0) bPoint = QPushButton('.') bPoint.clicked.connect(self.def_bPoint) grid.addWidget(bPoint, 6, 1) bsun = QPushButton('+') bsun.clicked.connect(self.def_bsun) grid.addWidget(bsun, 6, 3) bsun = QPushButton('=') bsun.clicked.connect(self.def_bresult) grid.addWidget(bsun, 6, 2) self.setGeometry(300, 300, 250, 250) self.show() ############################################################# def def_bc(self): self.info = 0 self.clu = 0 self.statusSUN = False self.lcd.display(self.info) def def_bk(self): if len(str(self.info)) > 1: self.info = int(str(self.info)[:-1]) else: self.info = 0 self.lcd.display(self.info) def def_b7(self): if self.info == 0: self.info = 7 else: self.info = int(str(self.info) + '7') self.statusSUN = False self.lcd.display(self.info) def def_b8(self): if self.info == 0: self.info = 8 else: self.info = int(str(self.info) + '8') self.statusSUN = False self.lcd.display(self.info) def def_b9(self): if self.info == 0: self.info = 9 else: self.info = int(str(self.info) + '9') self.statusSUN = False self.lcd.display(self.info) # 除 def def_bExcept(self): pass def def_b4(self): if self.info == 0: self.info = 4 else: self.info = int(str(self.info) + '4') self.statusSUN = False self.lcd.display(self.info) def def_b5(self): if self.info == 0: self.info = 5 else: self.info = int(str(self.info) + '5') self.statusSUN = False self.lcd.display(self.info) def def_b6(self): if self.info == 0: self.info = 6 else: self.info = int(str(self.info) + '6') self.statusSUN = False self.lcd.display(self.info) # 乘 def def_bMultiply(self): pass def def_b1(self): if self.info == 0: self.info = 1 else: self.info = int(str(self.info) + '1') self.statusSUN = False self.lcd.display(self.info) def def_b2(self): if self.info == 0: self.info = 2 else: self.info = int(str(self.info) + '2') self.statusSUN = False self.lcd.display(self.info) def def_b3(self): if self.info == 0: self.info = 3 else: self.info = int(str(self.info) + '3') self.statusSUN = False self.lcd.display(self.info) # 減 def def_bLess(self): pass def def_b0(self): if self.info == 0: self.info = 0 else: self.info = int(str(self.info) + '0') self.statusSUN = False self.lcd.display(self.info) def def_bPoint(self): if self.statusPoint: pass else: self.statusPoint = True if self.info == 0: self.info = 0. else: self.info = int(str(self.info) + '.') self.statusSUN = False self.lcd.display(self.info) # 等于,根據(jù)self.status判斷輸入的計(jì)算符號(hào) def def_bresult(self): self.clu += self.info self.lcd.display(self.clu) self.statusSUN = False self.info = 0 # 加 def def_bsun(self): self.clu += self.info self.lcd.display(self.clu) self.statusSUN = True self.info = 0 if __name__ == '__main__': app = QApplication(sys.argv) fe = ForExample() sys.exit(app.exec_())
看完上述內(nèi)容,你們對(duì)利用python怎么編寫一個(gè)計(jì)算器功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站題目:利用python怎么編寫一個(gè)計(jì)算器功能-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://jinyejixie.com/article20/ggeco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站建設(shè)、外貿(mào)建站、面包屑導(dǎo)航、建站公司、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容