Python PyQt Convert Pounds To Kilograms – To create a PyQt GUI program that converts lbs to Kgs, you can use a similar approach as in the previous examples. Here’s a basic PyQt program to perform this conversion:
Source Code
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton class PoundsToKilogramsConverter(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Pounds to Kilograms Converter') self.setGeometry(100, 100, 300, 150) # Create a vertical layout layout = QVBoxLayout() # Create input field for pounds self.pounds_label = QLabel('Enter pounds:') self.pounds_input = QLineEdit() # Create a label to display the result self.result_label = QLabel('Kilograms:') # Create a button to perform the conversion self.convert_button = QPushButton('Convert') # Connect the button click event to the conversion function self.convert_button.clicked.connect(self.performConversion) # Add widgets to the layout layout.addWidget(self.pounds_label) layout.addWidget(self.pounds_input) layout.addWidget(self.convert_button) layout.addWidget(self.result_label) # Set the layout for the main window self.setLayout(layout) def performConversion(self): try: # Get the pounds value from the input field pounds = float(self.pounds_input.text()) # Perform the conversion: 1 pound = 0.45359237 kilograms kilograms = pounds * 0.45359237 # Display the result self.result_label.setText(f'Kilograms: {kilograms:.2f}') except ValueError: # Handle invalid input (e.g., non-numeric input) self.result_label.setText('Invalid input') def main(): app = QApplication(sys.argv) window = PoundsToKilogramsConverter() window.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
This code creates a PyQt application with a window containing an input field for the user to enter pounds, a button to perform the conversion, and a label to display the result in kilograms. When the user enters a value in pounds and clicks the “Convert” button, the conversion is performed and displayed in the label.
Note: Make sure you have PyQt5 installed (pip install PyQt5) before running this code.
- Common PyQt GUI Widgets
- PyQt Introduction and Features
- Basic Structure of PyQt GUI Program
- Python PyQt Convert Kilograms To Pounds
- Python PyQt Find Factorial
- Python PyQt Add Two Numbers