Qt6 QtCreator で計算アプリを作成

プロジェクト calcapp 作成

calcapp.pyproject calcapp.py form.ui を作成

form.ui は、pyside6-uic form.ui -o ui_form.py を実行して ui_form.py を作成します。

form.ui

デザイナで作成します。その後、ui_form.py へ変換します

calcapp.py

# This Python file uses the following encoding: utf-8
import sys

from PySide6.QtWidgets import QApplication,QMainWindow,QMessageBox

# Important:
# You need to run the following command to generate the ui_form.py file
#     pyside6-uic form.ui -o ui_form.py, or
#     pyside2-uic form.ui -o ui_form.py
from ui_form import Ui_calcapp

class calcapp(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_calcapp()
        self.ui.setupUi(self)
        #self.setWindowTitle('計 算 機')
        self.ui.radioButton.setChecked(True)
        self.ui.pushButton.clicked.connect(self.executeCalc)
        self.ui.pushButton_2.clicked.connect(self.clear)
        self.ui.pushButton_3.clicked.connect(self.exit)
        self.ui.pushButton.setStyleSheet("background-color: yellow")
        self.ui.pushButton_2.setStyleSheet("background-color: white")
        self.ui.pushButton_3.setStyleSheet("background-color: blue")
        self.ui.lineEdit.setStyleSheet("background-color: aqua")
        self.ui.lineEdit_2.setStyleSheet("background-color: aqua")
        self.ui.lineEdit_3.setStyleSheet("background-color: pink")

    def exit(self) -> None:
                 sys.exit()
                 #self.close()

    def clear(self) -> None:
                 self.ui.lineEdit.setText('')
                 self.ui.lineEdit_2.setText('')
                 self.ui.lineEdit_3.setText('')

    def executeCalc(self):
                 value1 = self.ui.lineEdit.text()
                 value2 = self.ui.lineEdit_2.text()
                 error_message = ''
                 if value1 == '' or value2 == '':
                   error_message = '入力値が空です'
                 elif self.ui.radioButton_4.isChecked() == True and float(value2) == 0:
                   error_message = '0で割ることはできません'
                 else:
                   try:
                      float(value1)
                      float(value2)
                   except ValueError:
                      error_message = '入力値が数字ではありません'

                 ans = ''
                 if error_message == '':
                   if self.ui.radioButton.isChecked() == True:
                      ans = str(float(value1) + float(value2))
                   elif self.ui.radioButton_2.isChecked() == True:
                      ans = str(float(value1) - float(value2))
                   elif self.ui.radioButton_3.isChecked() == True:
                      ans = str(float(value1) * float(value2))
                   elif self.ui.radioButton_4.isChecked() == True:
                      ans = str(float(value1) / float(value2))
                   else:
                      pass

                 else:
                   QMessageBox.warning(self,'エラー',error_message)

                 self.ui.lineEdit_3.setText(ans)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = calcapp()
    form.show()
    sys.exit(app.exec())

QtCreator 内で実行すると

端末内では、 python3 calcapp.py で動作します。 pyside6 は、導入しておくこと。

PAGE TOP