Python PyQt5實現的簡易計算機功能樣本,pythonpyqt5

來源:互聯網
上載者:User

Python PyQt5實現的簡易計算機功能樣本,pythonpyqt5

本文執行個體講述了Python PyQt5實現的簡易計算機功能。分享給大家供大家參考,具體如下:

這裡剩下計算函數(self.calculator)未實現,有興趣的朋友可以實現它

【知識點】

1、利用迴圈添加按鈕組件,及給每個按鈕設定訊號/槽
2、給按鈕設定固定大小:button.setFixedSize(QtCore.QSize(60,30))
3、取事件的的寄件者(此例為各個按鈕)的文本: self.sender().text()

【】

【原始碼】

import sysfrom PyQt5 import QtWidgets,QtCore,QtGuiclass Example(QtWidgets.QWidget): def __init__(self):  super(Example, self).__init__()  self.initUI()  self.reset() def initUI(self):  self.setWindowTitle('簡易計算機')  grid = QtWidgets.QGridLayout()  self.display = QtWidgets.QLineEdit('0')  self.display.setFont(QtGui.QFont("Times", 20))  self.display.setReadOnly(True)  self.display.setAlignment(QtCore.Qt.AlignRight)  self.display.setMaxLength(15)  grid.addWidget(self.display,0,0,1,4)  names = ['Clear', 'Back', '', 'Close',     '7', '8', '9', '/',    '4', '5', '6', '*',     '1', '2', '3', '-',    '0', '.', '=', '+']  pos = [(0, 0), (0, 1), (0, 2), (0, 3),    (1, 0), (1, 1), (1, 2), (1, 3),    (2, 0), (2, 1), (2, 2), (2, 3),    (3, 0), (3, 1), (3, 2), (3, 3 ),    (4, 0), (4, 1), (4, 2), (4, 3)]  c = 0  for name in names:   button = QtWidgets.QPushButton(name)   button.setFixedSize(QtCore.QSize(60,30))   button.clicked.connect(self.buttonClicked) # 給每個按鈕設定訊號/槽   if c == 2:    pass    #grid.addWidget(QtWidgets.QLabel(''), 0, 2) #替換 第三個按鈕 為 文字標籤!   else:     grid.addWidget(button, pos[c][0]+1, pos[c][1])   c = c + 1  self.setLayout(grid) def buttonClicked(self):   #sender = self.sender(); # 確定訊號寄件者  #self.display.setText(sender.text())  text = self.sender().text()  if text in '+-*/':   self.history.append(self.number) # 數字入棧   self.history.append(text) # 運算子入棧   self.operator = text # 設定當前運算子   self.number = "" # 數字清空   self.numberType = "int"   return  elif text == "=":   self.calculate() # 計算  elif text == "Back":   pass  elif text == "Clear":   self.reset()  elif text == "Close":   self.close()  elif text == ".":   if self.numberType == "int":    self.number += text    self.numberType = "float"  else:   self.number = self.number + text if self.number != "0" else text  self.display.setText(self.number) def calculate(self):  pass def reset(self):  self.number = "0"  self.result = 0  self.history = []  self.operator = '' # +,-,*,/  self.numberType = 'int' # int與float兩種,如果輸入了小數點則為實數app = QtWidgets.QApplication(sys.argv)ex = Example()ex.show()sys.exit(app.exec_())

PS:這裡再為大家推薦幾款計算工具供大家進一步參考借鑒:

線上一元函數(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學計算機線上使用_進階計算機線上計算:
http://tools.jb51.net/jisuanqi/jsqkexue

線上計算機_標準計算機:
http://tools.jb51.net/jisuanqi/jsq

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.