寫好了精簡版本的計算機程式

昨日的計算機程式今日改了介面並在學長的幫助下完成了

啟動程式

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from cal import MainWindow

if __name__=='__main__':
    import sys
    app = QApplication(sys.argv)
    run = MainWindow()
    run.show()
    sys.exit(app.exec_())

計算主要程式

# -*- coding: utf-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from Ui_cal import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow): 
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

    @pyqtSlot()
    def on_plusButton_clicked(self):
        A = self.doubleSpinBoxA.value()
        B = self.doubleSpinBoxB.value()
        self.listWidget.addItem(str(A+B))

    @pyqtSlot()
    def on_clearButton_clicked(self):
        for i in range(self.listWidget.count()):
            self.listWidget.takeItem(0)

    @pyqtSlot()
    def on_minusButton_clicked(self):
        A = self.doubleSpinBoxA.value()
        B = self.doubleSpinBoxB.value()
        self.listWidget.addItem(str(A-B))

    @pyqtSlot()
    def on_divButton_clicked(self):
        try:
            A = self.doubleSpinBoxA.value()
            B = self.doubleSpinBoxB.value()
            self.listWidget.addItem(str(A/B))
        except: self.listWidget.addItem("Error!!")

    @pyqtSlot()
    def on_crossButton_clicked(self):
        A = self.doubleSpinBoxA.value()
        B = self.doubleSpinBoxB.value()
        self.listWidget.addItem(str(A*B))

經過詢問之後知道了self的用法

是代表在Mainwindow視窗底下中的圖塊

在完成後學長提出關於除零錯誤

我還想了一下並不是很懂為什麼會有bug

原來是因為會得出無限大的答案

錄製_2017_07_26_17_05_10_483 from Willy Lai on Vimeo.


Comments

comments powered by Disqus