欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5實(shí)現(xiàn)簡單的計算器

 更新時間:2020年05月30日 11:01:29   作者:伯納烏的斯坦森  
這篇文章主要為大家詳細(xì)介紹了PyQt5實(shí)現(xiàn)簡單的計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了PyQt5實(shí)現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下

下面我們將介紹使用python的PyQt5圖形界面來編寫一個簡易的計算器,實(shí)現(xiàn)“加,減,乘,除,平方,開方”等運(yùn)算。

代碼如下:

from PyQt5.QtGui import *
from PyQt5.Qt import *
from PyQt5.QtCore import *
import sys,math,string

class Calculator(QWidget):
  def __init__(self,parent=None):
    QWidget.__init__(self,parent=parent)
    self.initUI()
    self.last=[]
  def initUI(self):
    list=['&','**','s','C',7,8,9,'+',4,5,6,'-',1,2,3,'*',0,'.','=','/']
    length=len(list)
    #創(chuàng)建動態(tài)按鈕
    for i in range (length):
      self.button=QPushButton(str(list[i]),self)
      #將按鈕的clicked信號與onButtonClick函數(shù)相連
      self.button.clicked.connect(self.onButtonClick)
      x=i%4
      y=int(i/4)
      self.button.move(x*40+10,y*40+100)
      self.button.resize(30,30)
    #創(chuàng)建文本框
    self.lineEdit=QLineEdit('',self)
    self.lineEdit.move(10,10)
    self.lineEdit.resize(150,70)
    self.setGeometry(200,200,170,300)
    self.setWindowTitle('計算器')
    self.show()
  def onButtonClick(self):
    t=self.lineEdit.text()#獲取文本框文本
    new=self.sender().text()
    self.last.append(new)
    print(self.last)
    self.lineEdit.setText(t+new)
    if new== "=":
      result=eval(str(t))#計算
      self.lineEdit.setText(str(result))
    if new=='C':
      self.lineEdit.setText('')
    if new=='sqrt':
      self.lineEdit.setText('')
      result=math.sqrt(string.atof(t))
      self.lineEdit.setText(str(result))
    if new=="**":
      self.lineEdit.setText('')
      result=string.atof(t)**2
      self.lineEdit.setText(str(result))

app=QApplication(sys.argv)
w=Calculator()
w.show()
sys.exit(app.exec_())

實(shí)現(xiàn)界面如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論