1. Présentation du projet
Étape 1
Reprendre l'article précédent: Convertisseur de devises en Python et enregistrer le code qui contient les méthodes: get_exchange_rates() et convert_currency()dans un fichier python exchangeConverter.py au sein d'un dossier nommé currency-converter/
Étape 2
A l'aide de Qt Designer, créer l'interface library.ui suivante qui demande à l'utilisateur de
- Choisir la devise d'entrée (base currency ) et la devise de sortie ( Target Currency) via un widget QComboBox,
- Un champ QLineEdit() pour saisir la valeur de la devise à convertir (base currency) et un autre champ QLineEdit() destiné à afficher la valeur de la devise convertie (Target Value)
- Un QLabel nommée lbl_result destiné à afficher le taux de change de la devise
- Finalement un QPushButton nommé btn_validate pour valider l'opération et un autre btn_cancel pour annuler l'opération comme le montre la figure suivante tout en respectant le nommage des widgets:
- Widget N° 1 : comboBox_base_currency
- Widget N° 2 : comboBox_target_currency
- Widget N° 3 : lineEdit_base_currency
- Widget N° 4 : lineEdit_target_value
- Widget N° 5 : lbl_result
- Widget N° 6 : btn_validate
- Widget N° 7 : btn_cancel
- Créer le fichier library.py : à l'aide de l'invite de commande cmd, naviguer jusqu'au fichier library.ui et taper la commande:
1 |
[code lang="python"]pyuic5 -x library.ui -o library.py[/code] |
Cette dernière génerera un fichier library.py qui contient le code de l'interface utilisateur.
Vous obtenez ainsi l'arborescence finale du projet:
1 2 3 4 5 |
[code lang="python"]currency-converter/ exchangeConverter.py library.py library.ui main.py[/code] |
Étape 3
Créer un autre fichier nommé main.py sur lequel :
- Importer le contenu des deux fichiers library.py et exchangeConverter.py
- Codez la méthode btn_action() qui réalise l'action du bouton btn_validate.
2. Solution
Étape 1
Code du fichier exchangeConverter.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[code lang="python"]import requests def get_exchange_rates(base_currency): url = f"https://api.exchangerate-api.com/v4/latest/{base_currency}" response = requests.get(url) if response.status_code == 200: return response.json() else: return None def convert_currency(base_currency, target_currency, amount): data = get_exchange_rates(base_currency) if data: rates = data['rates'] if target_currency in rates: rate = rates[target_currency] converted_amount = amount * rate return converted_amount else: print(f"Taux de conversion pour {target_currency} non disponible.") return None else: print("Erreur lors de la récupération des taux de change.") return None[/code] |
Étape 2
Code du fichier library.py généré par Qt Designer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
[code lang="python"]from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(622, 343) self.verticalLayoutWidget = QtWidgets.QWidget(Form) self.verticalLayoutWidget.setGeometry(QtCore.QRect(-20, 0, 681, 81)) self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget) font = QtGui.QFont() font.setPointSize(28) font.setBold(True) font.setWeight(75) self.label_2.setFont(font) self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight) self.label_2.setAutoFillBackground(False) self.label_2.setStyleSheet("background-color: rgb(13, 32, 141);\n" "color: rgb(255, 255, 255);") self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) self.btn_validate = QtWidgets.QPushButton(Form) self.btn_validate.setGeometry(QtCore.QRect(370, 290, 231, 31)) font = QtGui.QFont() font.setFamily("Verdana") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.btn_validate.setFont(font) self.btn_validate.setStyleSheet("color: rgb(0, 0, 255);") self.btn_validate.setObjectName("btn_validate") self.label_7 = QtWidgets.QLabel(Form) self.label_7.setGeometry(QtCore.QRect(130, 230, 191, 20)) font = QtGui.QFont() font.setPointSize(12) font.setBold(False) font.setWeight(50) self.label_7.setFont(font) self.label_7.setStyleSheet("color: rgb(255, 0, 0);") self.label_7.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.label_7.setObjectName("label_7") self.lbl_result = QtWidgets.QLabel(Form) self.lbl_result.setGeometry(QtCore.QRect(350, 230, 211, 20)) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.lbl_result.setFont(font) self.lbl_result.setStyleSheet("color: rgb(255, 0, 0);") self.lbl_result.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.lbl_result.setObjectName("lbl_result") self.line = QtWidgets.QFrame(Form) self.line.setGeometry(QtCore.QRect(20, 250, 581, 20)) self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.btn_cancel = QtWidgets.QPushButton(Form) self.btn_cancel.setGeometry(QtCore.QRect(130, 290, 231, 31)) font = QtGui.QFont() font.setFamily("Verdana") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.btn_cancel.setFont(font) self.btn_cancel.setStyleSheet("color: rgb(0, 0, 255);") self.btn_cancel.setObjectName("btn_cancel") self.line_2 = QtWidgets.QFrame(Form) self.line_2.setGeometry(QtCore.QRect(20, 210, 581, 20)) self.line_2.setFrameShape(QtWidgets.QFrame.HLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.layoutWidget = QtWidgets.QWidget(Form) self.layoutWidget.setGeometry(QtCore.QRect(20, 100, 581, 99)) self.layoutWidget.setObjectName("layoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.lineEdit_target_value = QtWidgets.QLineEdit(self.layoutWidget) self.lineEdit_target_value.setObjectName("lineEdit_target_value") self.gridLayout.addWidget(self.lineEdit_target_value, 1, 3, 1, 1) self.label_3 = QtWidgets.QLabel(self.layoutWidget) font = QtGui.QFont() font.setPointSize(12) font.setBold(False) font.setWeight(50) self.label_3.setFont(font) self.label_3.setStyleSheet("color: rgb(0, 0, 255);") self.label_3.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.label_3.setObjectName("label_3") self.gridLayout.addWidget(self.label_3, 1, 0, 1, 1) self.label_6 = QtWidgets.QLabel(self.layoutWidget) font = QtGui.QFont() font.setPointSize(12) font.setBold(False) font.setWeight(50) self.label_6.setFont(font) self.label_6.setStyleSheet("color: rgb(0, 0, 255);") self.label_6.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.label_6.setObjectName("label_6") self.gridLayout.addWidget(self.label_6, 1, 2, 1, 1) self.label_5 = QtWidgets.QLabel(self.layoutWidget) font = QtGui.QFont() font.setPointSize(12) font.setBold(False) font.setWeight(50) self.label_5.setFont(font) self.label_5.setStyleSheet("color: rgb(0, 0, 255);") self.label_5.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.label_5.setObjectName("label_5") self.gridLayout.addWidget(self.label_5, 0, 2, 1, 1) self.lineEdit_base_currency = QtWidgets.QLineEdit(self.layoutWidget) self.lineEdit_base_currency.setObjectName("lineEdit_base_currency") self.gridLayout.addWidget(self.lineEdit_base_currency, 0, 3, 1, 1) self.comboBox_target_currency = QtWidgets.QComboBox(self.layoutWidget) self.comboBox_target_currency.setObjectName("comboBox_target_currency") self.gridLayout.addWidget(self.comboBox_target_currency, 1, 1, 1, 1) self.label = QtWidgets.QLabel(self.layoutWidget) font = QtGui.QFont() font.setPointSize(12) font.setBold(False) font.setWeight(50) self.label.setFont(font) self.label.setStyleSheet("color: rgb(0, 0, 255);") self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.comboBox_base_currency = QtWidgets.QComboBox(self.layoutWidget) self.comboBox_base_currency.setObjectName("comboBox_base_currency") self.gridLayout.addWidget(self.comboBox_base_currency, 0, 1, 1, 1) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label_2.setText(_translate("Form", " Currency Converter")) self.btn_validate.setText(_translate("Form", "Validate")) self.label_7.setText(_translate("Form", "Currency Exchange Rate :")) self.lbl_result.setText(_translate("Form", "...")) self.btn_cancel.setText(_translate("Form", "Cancel")) self.label_3.setText(_translate("Form", "Tarjet Currency ")) self.label_6.setText(_translate("Form", " Target Value ")) self.label_5.setText(_translate("Form", " Amount")) self.label.setText(_translate("Form", "Base Currency"))[/code] |
Étape 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[code lang="python"]import sys from library import * from exchangeConverter import * def btn_action(): base_currency = ui.comboBox_base_currency.currentText() target_currency = ui.comboBox_target_currency.currentText() amount = float(ui.lineEdit_base_currency.text()) convert_value = convert_currency(base_currency, target_currency, amount) convert_value = str(convert_value) ui.lineEdit_target_value.setText(convert_value) # Compute currency exchange rate exchange_rate = str(convert_currency(base_currency, target_currency, 1)) ui.lbl_result.setText(exchange_rate) app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) data_exchange = get_exchange_rates('USD')['rates'] ui.comboBox_base_currency.addItems(list(data_exchange.keys())) ui.comboBox_target_currency.addItems(list(data_exchange.keys())) base_currency = ui.lineEdit_base_currency.text() target_currency = ui.lineEdit_target_value.text() ui.btn_validate.clicked.connect(btn_action) Form.show() sys.exit(app.exec_())[/code] |
Younes Derfoufi
CRMEF OUJDA
Acheter sur Très Facile !