0

I'm trying to design a user interface for a currency conversion script I wrote before. I couldn't figure out how to send the inputs I get from the user to the function. I have added the function as it works in the terminal. I have left the necessary parts of the code below. Thank you for all the help

import requests
import sys

from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        
        self.init_ui()
        
    def init_ui(self):
       
        # from / to / amount text features
        le_text_edit = QtGui.QFont()
        le_text_edit.setFamily("Arial")
        le_text_edit.setPointSize(10)
        le_text_edit.setBold(True)
        le_text_edit.setWeight(75)
        
        # from
        self.from_le = QtWidgets.QLineEdit(self)
        self.from_le.setGeometry(QtCore.QRect(82, 60, 101, 21))
        
        self.from_le.setFont(le_text_edit)
        self.from_le.setReadOnly(True)
        self.from_le.setText("From:")
        
        # To
        self.to_le = QtWidgets.QLineEdit(self)
        self.to_le.setGeometry(QtCore.QRect(82, 90, 101, 21))
        
        self.to_le.setFont(le_text_edit)
        self.to_le.setReadOnly(True)
        self.to_le.setText("To:")
        
        # amount
        self.amount_le = QtWidgets.QLineEdit(self)
        self.amount_le.setGeometry(QtCore.QRect(82, 120, 101, 21))
        
        self.amount_le.setFont(le_text_edit)
        self.amount_le.setReadOnly(True)
        self.amount_le.setText("Amount:")
        
        # from / To / amount inputs text features
        input_text_edit = QtGui.QFont()
        input_text_edit.setFamily("Arial")
        input_text_edit.setPointSize(10)
        
        # from input
        self.from_input = QtWidgets.QLineEdit(self)
        self.from_input.setGeometry(QtCore.QRect(190, 60, 232, 21))
        
        self.from_input.setFont(input_text_edit)
        
        # to input
        self.to_input = QtWidgets.QLineEdit(self)
        self.to_input.setGeometry(QtCore.QRect(190, 90, 232, 21))
        
        self.to_input.setFont(input_text_edit)
        
        # amount input
        self.amount_input = QtWidgets.QLineEdit(self)
        self.amount_input.setGeometry(QtCore.QRect(190, 120, 232, 21))
        
        self.amount_input.setFont(input_text_edit)

        # button text features
        button_text = QtGui.QFont()
        button_text.setFamily("Arial")
        button_text.setPointSize(9)
        
        # push buttons
        self.convert_button = QtWidgets.QPushButton(self)
        self.convert_button.setGeometry(QtCore.QRect(252, 180, 170, 25))
        self.convert_button.setText("Convert")
        self.convert_button.setFont(button_text)
        
        self.clear_button = QtWidgets.QPushButton(self)
        self.clear_button.setGeometry(QtCore.QRect(82, 180, 170, 25))
        self.clear_button.setText("Clear")
        self.clear_button.setFont(button_text)
       
        # button functions
        self.convert_button.clicked.connect(self.convert_currency)
        self.clear_button.clicked.connect(self.clear_all)
        
        self.setWindowTitle("Currency Converter App")
        self.setGeometry(500, 500, 250, 300)
        self.show()
        
    def convert_currency():
        url = "http://data.fixer.io/api/latest?access_key=b3f30857f7e75c205e54389105692772&currencies=EUR"

        base_currency = "EUR"
        first_currency = input("From: ")
        second_currency = input("To: ")
        amount = float(input("Amount: "))

        if first_currency == base_currency:
            response = requests.get(url + first_currency)
            json_data = response.json()
            try:
                print(json_data["rates"][second_currency] * amount)
            except KeyError:
                sys.stderr.write("Invalid Currency!")
                sys.stderr.flush()
        elif first_currency != base_currency:
            response = requests.get(url + base_currency)
            json_data = response.json()
            try:
                data = json_data["rates"][second_currency]
                data2 = json_data["rates"][first_currency]
                result = data / data2
                print(result * amount)
            except KeyError:
                sys.stderr.write("Invalid Currency!")
                sys.stderr.flush()
                
    def clear_all(self):
        self.from_input.clear()
        self.to_input.clear()
        self.amount_input.clear()
        
        
app = QtWidgets.QApplication(sys.argv)

window = Window()

sys.exit(app.exec_())
Lenny
  • 1
  • 2
  • You are looking for the function `QLineEdit.text()` : Simply use `amount = amount_le.text()`. – Demi-Lune Feb 21 '22 at 13:57
  • It's also sometimes more interesting to use the associated signal: `QLineEdit.textChanged` – Demi-Lune Feb 21 '22 at 13:58
  • It worked. TYSM – Lenny Feb 21 '22 at 14:13
  • More detailed answers [here](https://stackoverflow.com/questions/42288320/python-how-to-get-qlineedit-text), and [there for textChanged](https://stackoverflow.com/questions/22531578/make-an-action-when-the-qlineedit-text-is-changed-programmatically) – Demi-Lune Feb 21 '22 at 14:21

0 Answers0