在實驗室已重新灌window系統的電腦上安裝以及撰寫計算機程式

有上次的經驗這次安裝過程快了很多

不過實驗室的電腦所接收到的網路速度沒有破過200kB

所以安裝Qt的過程是從昨天開始

讓電腦在實驗室跑了一天

今日安裝完成後

學長有教學關於class的概念

class dog:
    def __init__(self):
        print("bark!")

    def walk(self, x):
        print("walk %d"%x)

class black_dog(dog):
    def __init__(self, a):
        print(a)

    def black_dog_walk(self, x):
        print("black dog walk %d"%x)

    def walk(self, x):
        super(black_dog, self).walk(x)
        self.black_dog_walk(x)

BD = black_dog(10)
BD.walk(5)

以上列的狗狗與黑狗跑幾步來說明

"繼承"程式的概念

如中間段的class black_dog(dog):即是以黑色狗狗繼承一開始dog的函式

但由於blackdog底下沒有執行dog函式中列印字串的功能 故bark不會被列印出來

並簡單展示了列應字串的基礎程式

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

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

    @pyqtSlot()
    def on_AButton_clicked(self):
        print("Hello world!")

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

所有程式基礎最愛用字眼"Hello World"

學長出了一個計算機的功課

不過只要求我寫出有加法的即可

但由於不熟悉

只寫出部分功能


# -*- 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)
        self.floatMod = False

    @pyqtSlot()
    def on_one_clicked(self): self.add_num(1)
    @pyqtSlot()
    def on_seven_clicked(self): self.add_num(7)
    @pyqtSlot()
    def on_three_clicked(self): self.add_num(3)
    @pyqtSlot()
    def on_four_clicked(self): self.add_num(4)
    @pyqtSlot()
    def on_nine_clicked(self): self.add_num(9)
    @pyqtSlot()
    def on_six_clicked(self): self.add_num(6)
    @pyqtSlot()
    def on_five_clicked(self): self.add_num(5)
    @pyqtSlot()
    def on_two_clicked(self): self.add_num(2)
    @pyqtSlot()
    def on_eight_clicked(self): self.add_num(8)
    @pyqtSlot()
    def on_zero_clicked(self): self.add_num(0)

    def add_num(self, x):
        val = self.lcd.value()
        if val==0:
            self.lcd.display(x)
        elif self.floatMod:
            float_len = len(str(val).split('.')[-1])
            self.lcd.display(val+x*10**-float_len)
        else:
            self.lcd.display(val*10+x)

    @pyqtSlot()
    def on_plus_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        raise NotImplementedError

    @pyqtSlot()
    def on_equal_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        raise NotImplementedError

    @pyqtSlot()
    def on_dot_clicked(self):
        if self.floatMod==False:
            self.floatMod = True

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

最大重要問題

小數點(del)屬Python的語言

故不能取del為名稱

程式圖樣


Comments

comments powered by Disqus