使用 PyQt5 在 Python 中構建 1/4 英里計算器
1/4 英里的直線加速賽是評估汽車和摩托車效能的一種常見指標。發燒友和專家都使用此距離來評估加速度和整體效能。在本文中,我們將使用 PyQt5(一個著名的 Python 庫,用於建立圖形使用者介面 (GUI))構建一個基本的 1/4 英里估算器。在本文結束時,您將擁有一個功能齊全的 1/4 英里估算器,可用於評估各種車輛的效能。
為什麼選擇 PyQt5 作為 1/4 英里估算器?
PyQt5 是一個功能強大且通用的庫,用於在 Python 中構建桌面應用程式。它提供了一組直觀的工具來建立高階、使用者友好的 GUI,這些 GUI 可以在各種平臺上執行,包括 Windows、macOS 和 Linux。PyQt5 特別適合開發 1/4 英里估算器,因為它易於使用、跨平臺相容且文件齊全。
使用 PyQt5 在 Python 中構建 1/4 英里估算器的步驟
安裝 PyQt5
在開始之前,我們需要安裝 PyQt5。您可以使用 Python 包安裝程式 pip 來完成此操作,方法是執行以下命令:
pip install PyQt5
匯入必要的模組
首先,讓我們匯入基本 PyQt5 模組和 Python 的內建 math 模組。
import sys import math from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton
開發主應用程式類
接下來,為主要應用程式視窗建立一個類,該類繼承自 QWidget。此類將包含我們估算器的元件和佈局。
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_())
定義繼承自 QWidget 的 QuarterMileEstimator 類。定義 init 方法來初始化物件並呼叫 init_ui 方法。
在 init_ui 方法中,建立和配置 UI 元素,例如標籤、輸入欄位和按鈕。將 UI 元素新增到 QVBoxLayout 佈局中。
將估計按鈕的 clicked 訊號連線到 estimate_quarter_mile 方法,我們將在下一階段定義該方法。設定 QuarterMileEstimator 視窗部件的佈局。
實現 1/4 英里估算
現在,讓我們將估算邏輯整合到我們的估算器中。我們將使用以下公式來近似 1/4 英里的時間:
time (seconds) = 6.269 * sqrt(weight (lbs) / horsepower)
在 QuarterMileEstimator 類中建立 estimate_quarter_mile 方法來執行此估算:
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.')
在 QuarterMileEstimator 類中定義 estimate_quarter_mile 方法。
從輸入欄位檢索重量和馬力值,並將它們轉換為浮點數。使用公式計算估計的 1/4 英里的時間。
在 result_label QLabel 中顯示結果。如果發生 ValueError(例如,如果輸入欄位包含非數字值),則顯示錯誤訊息。
設定主應用程式迴圈
最後,建立主應用程式迴圈來執行估算器:
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_())
檢查指令碼是否作為主程式執行(即未作為模組匯入)。建立一個 QApplication 物件,並將命令列引數傳遞給它。
建立一個 QuarterMileEstimator 類的例項。設定視窗標題並使用 show 方法顯示估算器。
使用 app.exec_() 執行應用程式的事件迴圈,並在迴圈結束時退出指令碼。
輸出
--------------------------- | 1/4 Mile Estimator | --------------------------- | Weight (lbs): | | [_______________] | | Horsepower: | | [_______________] | | [Estimate] | | | | Approximated 1/4 Mile | | Time: ____ seconds | ---------------------------
結論
透過遵循這些步驟,您現在擁有了一個使用 PyQt5 在 Python 中構建的功能齊全的 1/4 英里估算器。這個簡單但功能強大的工具可用於根據車輛的重量和馬力來評估各種車輛的效能。使用 PyQt5,您可以輕鬆建立跨平臺的桌面應用程式,用於各種用例,從基本的估算器到複雜的生產力工具。
隨著您繼續提高 Python 和 PyQt5 技能,請考慮探索更高階的功能和技術,例如與資料庫整合、合併多媒體和建立自定義視窗部件。透過不斷學習和實踐,您將能夠勝任任何遇到的桌面應用程式專案。