Python PyQt Gallons To Liters Conversion GUI Program

By | October 23, 2023

Python PyQt Gallons To Liters Conversion GUI Program – This code creates a simple PyQt GUI program with a main window, a label, an input field, a “Convert” button, and a label to display the result. When you enter the number of gallons and click the “Convert” button, it calculates the equivalent amount in liters and displays the result in the lower label.

Python PyQt Gallons To Liters Conversion GUI Program

Python PyQt Gallons To Liters Conversion GUI Program

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout

class GallonsToLitersConverter(QMainWindow):
    def __init__(self):


    def initUI(self):
        self.setWindowTitle("Gallons to Liters Converter")
        self.setGeometry(100, 100, 300, 150)

        self.central_widget = QWidget()

        layout = QVBoxLayout()

        self.label = QLabel("Enter the number of gallons:")
        self.textbox = QLineEdit()
        self.convert_button = QPushButton("Convert")
        self.result_label = QLabel()




    def convert(self):
            gallons = float(self.textbox.text())
            liters = gallons * 3.78541
            self.result_label.setText(f"{gallons} gallons is equal to {liters:.2f} liters.")
        except ValueError:
            self.result_label.setText("Invalid input. Please enter a number.")

def main():
    app = QApplication(sys.argv)
    converter = GallonsToLitersConverter()

if __name__ == '__main__':

To create a PyQt GUI program that converts gallons to liters, you’ll need to have PyQt installed. If you haven’t already installed PyQt, you can do so using pip:

pip install PyQt5

This PyQt-based program creates a Graphical User Interface (GUI) for converting gallons to liters. Let’s break down the program step by step:

  1. Import necessary modules:
    • sys: Used for system-related operations.
    • QApplication, QMainWindow, QWidget, QLabel, QLineEdit, and QPushButton are PyQt5 classes for creating the GUI elements.
    • QVBoxLayout is a layout manager used to arrange widgets vertically.
  2. Create a class GallonsToLitersConverter that inherits from QMainWindow. This class represents the main window of the application.
  3. In the __init__ method, initialize the application by calling the initUI method.
  4. In the initUI method:
    • Set the window’s title and geometry (position and size).
    • Create the central widget for the main window.
    • Create layout manager QVBoxLayout to arrange the widgets vertically.
    • Create the following widgets:
      • label: A QLabel widget displaying the text “Enter the number of gallons:”.
      • textbox: A QLineEdit widget for entering the number of gallons.
      • convert_button: A QPushButton widget labeled “Convert”.
      • result_label: A QLabel widget for displaying the conversion result.
    • Connect the clicked signal of the convert_button to the convert method, which will be called when the button is clicked.
    • Add all the widgets to the layout and set it as the layout for the central widget.
  5. Create the convert method:
    • This method is called when the “Convert” button is clicked.
    • It attempts to convert the user’s input to a floating-point number (gallons), then calculates the equivalent volume in liters, and displays the result in the result_label.
    • If the input is not a valid number, it displays an error message in the result_label.
  6. Define the main function:
    • Create a PyQt5 application (QApplication).
    • Create an instance of the GallonsToLitersConverter class, which represents the main window.
    • Show the main window.
    • Start the PyQt event loop by calling app.exec_(), which allows the GUI to respond to user interactions.
  7. Finally, the code checks if it’s the main module (not imported as a library), and if so, it calls the main function to start the application.

When you run this program, it will open a GUI window with a label, an input field, and a button. Users can enter a number of gallons, click the “Convert” button, and see the conversion result in liters displayed below the button. If the input is not a valid number, it will show an error message.



Leave a Reply

Your email address will not be published. Required fields are marked *