1/4 Mile Calculator using PyQt5 in Python


The 1/4 mile drag sprint is a prevalent gauge for assessing the performance of automobiles and motorcycles. Aficionados and experts alike employ this distance to appraise acceleration and overall capabilities. In this piece, we will construct a basic 1/4 mile estimator using PyQt5, a renowned Python library for crafting graphical user interfaces (GUIs). By the conclusion of this piece, you'll possess a functional 1/4 mile estimator that can be employed to evaluate the performance of various vehicles.

Why choose PyQt5 for a 1/4 Mile Estimator?

PyQt5 is a strong and versatile library for building desktop applications in Python. It offers a collection of intuitive tools for producing advanced, user-friendly GUIs that can work on numerous stages, including Windows, macOS, and Linux. PyQt5 is particularly well-suited for developing a 1/4 mile estimator due to its user-friendliness, cross-platform compatibility, and broad documentation.

Phases to Construct a 1/4 Mile Estimator using PyQt5 in Python

Install PyQt5

Before commencing, we must install PyQt5. You can accomplish this using pip, the Python package installer, by executing the following command −

pip install PyQt5

Incorporate necessary modules

First, let's join the fundamental PyQt5 modules and Python's built-in math module.

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

Develop the primary application class

Subsequently, create a class for the primary application window that inherits from QWidget. This class will encompass the components and layout of our estimator.

class QuarterMileEstimator(QWidget): def init(self): super().init()

   # Initialize UI elements
   self.init_ui()

def init_ui(self):
   # Produce and configure UI elements
   layout = QVBoxLayout()

   self.title = QLabel('1/4 Mile Estimator')
   self.weight_label = QLabel('Weight (lbs):')
   self.weight_input = QLineEdit()
   self.hp_label = QLabel('Horsepower:')
   self.hp_input = QLineEdit()
   self.calculate_button = QPushButton('Estimate')
   self.result_label = QLabel('')

   # Append UI elements to the layout
   layout.addWidget(self.title)
   layout.addWidget(self.weight_label)
   layout.addWidget(self.weight_input)
   layout.addWidget(self.hp_label)
   layout.addWidget(self.hp_input)
   layout.addWidget(self.calculate_button)
   layout.addWidget(self.result_label)

   # Connect the estimate button to the calculation function
   self.calculate_button.clicked.connect(self.estimate_quarter_mile)

   # Set the layout for the widget
   self.setLayout(layout)

time (seconds) = 6.269 * sqrt(weight (lbs) / horsepower)
def estimate_quarter_mile(self):
   try:
      weight = float(self.weight_input.text())
      horsepower = float(self.hp_input.text())

      # Compute the 1/4 mile time
      time = 6.269 * math.sqrt(weight / horsepower)

      # Exhibit the result
      self.result_label.setText(f'Approximated 1/4 Mile Time: {time:.2f} seconds')
   except ValueError:
      # Exhibit an error message if the input is not valid
      self.result_label.setText('Error: Invalid input. Please enter valid numbers for weight and horsepower.')

if name == 'main': app = QApplication(sys.argv) estimator = QuarterMileEstimator()

estimator.setWindowTitle('1/4 Mile Estimator')
estimator.show()

sys.exit(app().exec_())
  • Define the QuarterMileEstimator class that inherits from QWidget. Define the init method to initialize the object and call the init_ui method.

  • In the init_ui method, produce and configure the UI elements, such as labels, input fields, and buttons. Add the UI elements to the QVBoxLayout layout.

  • Connect the estimate button's clicked signal to the estimate_quarter_mile method, which we will define in the next phase. Set the layout for the QuarterMileEstimator widget.

Implement the 1/4 mile estimation

Now, let's incorporate the estimation logic into our estimator. We will employ the following formula to approximate the 1/4 mile time −

time (seconds) = 6.269 * sqrt(weight (lbs) / horsepower)

Create the estimate_quarter_mile method in theQuarterMileEstimator class to execute this estimation −

def estimate_quarter_mile(self):
   try:
      weight = float(self.weight_input.text())
       horsepower = float(self.hp_input.text())

      # Compute the 1/4 mile time
      time = 6.269 * math.sqrt(weight / horsepower)

      # Exhibit the result
      self.result_label.setText(f'Approximated 1/4 Mile Time: {time:.2f} seconds')
   except ValueError:
      # Exhibit an error message if the input is not valid
      self.result_label.setText('Error: Invalid input. Please enter valid numbers for weight and horsepower.')
  • Define the estimate_quarter_mile method in the QuarterMileEstimator class.

  • Retrieve the weight and horsepower values from the input fields and convert them to floats. Utilize the formula to calculate the estimated 1/4 mile time.

  • Display the result in the result_label QLabel. If a ValueError occurs (e.g., if the input fields contain non-numeric values), display an error message.

Set up the primary application loop

Ultimately, create the primary application loop to operate the estimator −

Ultimately, create the primary application loop to operate the estimator:

if name == 'main': app = QApplication(sys.argv) estimator = QuarterMileEstimator()

estimator.setWindowTitle('1/4 Mile Estimator')
estimator.show()

sys.exit(app().exec_())
  • Verify in the event that the script is being executed as the main program (i.e., not being imported as a module). Make a QApplication object, passing within the command-line arguments.

  • Create an instance of the QuarterMileEstimator class. Set the window title and display the estimator using the show method.

  • Run the application's event loop with app.exec_(), and exit the script when the loop concludes.

Output

---------------------------
| 1/4 Mile Estimator      |
---------------------------
| Weight (lbs):           |
| [_______________]       |
| Horsepower:             |
| [_______________]       |
| [Estimate]              |
|                         |
| Approximated 1/4 Mile   |
| Time: ____ seconds      |
---------------------------

Conclusion

By adhering to these phases, you now possess a fully functional 1/4 mile estimator using PyQt5 in Python. This straightforward yet potent tool can be employed to evaluate the performance of various vehicles based on their weight and horsepower. With PyQt5, you can effortlessly create cross-platform desktop applications for a wide array of use cases, from basic estimators to intricate productivity tools.

As you continue to advance your Python and PyQt5 skills, consider exploring more sophisticated features and techniques, such as integrating with databases, incorporating multimedia, and crafting custom widgets. By constantly learning and experimenting, you'll be well-equipped to tackle any desktop application project that comes your way.

Updated on: 09-Aug-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements