×

Standard GUI Unit Converter using PyQt5 in Python

GUI:

A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast to text-based interfaces, which only display data and commands as text, GUIs visualize information and related user controls. A cursor, trackball, stylus, or thumb on a touch screen are all pointing devices that can be used to communicate with GUI representations. The first text interface between a human and a computer operated using keyboard input and a prompt.

PyQt5:

PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's capabilities and convenience, one can quickly create an interactive computer program.  Front-end and Back-end make up a GUI application. In order to speed up development & allow more time to be spent on back-end tasks, PyQt5 has developed a tool called "QtDesigner" that allows one to drag and drop elements to create the front end. Riverbank Computing is the company in charge of developing and maintaining PyQt.

Understanding the Unit Conversion:

The unit conversion process entails several steps involving addition, subtraction, or multiplication by a conversion factor. Additionally, rounding and choosing the appropriate number of substantial digits may be included in the procedure. Various conversion units are used to measure various properties.

Unit conversion is required when attempting to solve various mathematical puzzles. For instance, if a rectangle's length is given in feet, but its width is given in meters, we must convert the units to make them uniform before finding the rectangle's perimeter. As a result, we must understand the idea of unit conversion.

  1. Measuring Weight
  2. Measuring Capacity
  3. Measuring Length
  4. Measuring Temperature

Conversion of Units Measurements:

The different types of unit measurements are:

  1. Length
  2. Area
  3. Volume
  4. Weight
  5. Temperature

In the early stages of learning, some non-standard units are used to introduce kids to units and measurements without requiring them to read any scales. Since reading any scale is a tedious task in and of itself, the introduction of non-standard measures enables the child to concentrate on ideas like heavy, light, long, short, and more before progressing to the next stage of measuring using the standard units.

  • So, we calculate the unit conversions in this code using PyQt5 with the imported libraries which are present.

CODE:

Final Code:
from PyQt5 import QtCore, QtGui, QtWidgets




class Ui_UnitConverter(object):
    def setupUi(self, UnitConverter):
        UnitConverter.setObjectName("UnitConverter")
        UnitConverter.resize(728, 601)
        self.centralwidget = QtWidgets.QWidget(UnitConverter)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setContentsMargins(10, 10, 10, 10)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.dropShadowFrame = QtWidgets.QFrame(self.centralwidget)
        self.dropShadowFrame.setStyleSheet("QFrame{\n"
                                           "    background-color: rgb(44, 50, 86);\n"
                                           "    color: rgb(200, 200, 200);\n"
                                           "}")
        self.dropShadowFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.dropShadowFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.dropShadowFrame.setObjectName("dropShadowFrame")
        self.label = QtWidgets.QLabel(self.dropShadowFrame)
        self.label.setGeometry(QtCore.QRect(190, 50, 311, 41))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(18)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(79, 199, 255);")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.dropShadowFrame)
        self.lineEdit.setGeometry(QtCore.QRect(50, 200, 161, 31))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(7)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setStyleSheet("QLineEdit {\n"
                                    "background-color: rgb(115, 199, 255);\n"
                                    "border: none;\n"
                                    "border-radius: 9px;\n"
                                    "}")
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.dropShadowFrame)
        self.lineEdit_2.setGeometry(QtCore.QRect(360, 200, 161, 31))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(7)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setStyleSheet("QLineEdit {\n"
                                      "background-color: rgb(115, 199, 255);\n"
                                      "border: none;\n"
                                      "border-radius: 9px;\n"
                                      "}")
        self.lineEdit_2.setText("")
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.comboBox_1 = QtWidgets.QComboBox(self.dropShadowFrame)
        self.comboBox_1.setGeometry(QtCore.QRect(210, 200, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_1.setFont(font)
        self.comboBox_1.setStyleSheet("QComboBox {\n"
                                      "\n"
                                      "    background-color: rgb(44, 50, 86);\n"
                                      "    color: rgb(79, 199, 255);\n"
                                      "    border: none;\n"
                                      "    border-radius: 9px;\n"
                                      "    text -align: center;\n"
                                      "}\n"
                                      "")
        self.comboBox_1.setObjectName("comboBox_1")
        self.comboBox_1.addItem("Second[s]")
        self.comboBox_1.addItem("Millisecond[ms]")
        self.comboBox_1.addItem("Minute[m]")
        self.comboBox_1.addItem("Hour[h]")
        self.comboBox_1.addItem("Day[D]")
        self.comboBox_1.addItem("Year[y]")
        self.comboBox_2 = QtWidgets.QComboBox(self.dropShadowFrame)
        self.comboBox_2.setGeometry(QtCore.QRect(520, 200, 131, 31))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_2.setFont(font)
        self.comboBox_2.setStyleSheet("QComboBox {\n"
                                      "\n"
                                      "    background-color: rgb(44, 50, 86);\n"
                                      "    color: rgb(79, 199, 255);\n"
                                      "    border: none;\n"
                                      "    border-radius: 9px;\n"
                                      "    text -align: center;\n"
                                      "}\n"
                                      "")
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("Second[s]")
        self.comboBox_2.addItem("Millisecond[ms]")
        self.comboBox_2.addItem("Minute[m]")
        self.comboBox_2.addItem("Hour[h]")
        self.comboBox_2.addItem("Day[D]")
        self.comboBox_2.addItem("Year[y]")
        self.comboBox_3 = QtWidgets.QComboBox(self.dropShadowFrame)
        self.comboBox_3.setGeometry(QtCore.QRect(260, 450, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_3.setFont(font)
        self.comboBox_3.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.comboBox_3.setStyleSheet("QComboBox {\n"
                                      "\n"
                                      "    background-color: rgb(44, 50, 86);\n"
                                      "    color: rgb(79, 199, 255);\n"
                                      "    border: none;\n"
                                      "    border-radius: 9px;\n"
                                      "    text -align: center;\n"
                                      "}\n"
                                      "")
        self.comboBox_3.setEditable(False)
        self.comboBox_3.setObjectName("comboBox_3")
        self.comboBox_3.addItem("TIME")


        self.label_2 = QtWidgets.QLabel(self.dropShadowFrame)
        self.label_2.setGeometry(QtCore.QRect(90, 170, 81, 21))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(18)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: rgb(247, 19, 255);")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.dropShadowFrame)
        self.label_3.setGeometry(QtCore.QRect(390, 170, 81, 21))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(18)
        self.label_3.setFont(font)
        self.label_3.setStyleSheet("color: rgb(247, 19, 255);")
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.dropShadowFrame)
        self.label_4.setGeometry(QtCore.QRect(300, 390, 91, 41))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(18)
        self.label_4.setFont(font)
        self.label_4.setStyleSheet("color: rgb(247, 19, 255);")
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName("label_4")


        self.verticalLayout.addWidget(self.dropShadowFrame)
        UnitConverter.setCentralWidget(self.centralwidget)


        self.retranslateUi(UnitConverter)
        QtCore.QMetaObject.connectSlotsByName(UnitConverter)


    def retranslateUi(self, UnitConverter):
        _translate = QtCore.QCoreApplication.translate
        UnitConverter.setWindowTitle(_translate("UnitConverter", "MainWindow"))
        self.label.setText(_translate("UnitConverter",
                                      "<html><head/><body><p>MY <span style=\" font-weight:600;\">UNIT CONVERTER</span></p></body></html>"))


        self.label_2.setText(_translate("UnitConverter",
                                        "<html><head/><body><p><span style=\" font-size:10pt; font-weight:600;\">FROM:</span></p></body></html>"))
        self.label_3.setText(_translate("UnitConverter",
                                        "<html><head/><body><p><span style=\" font-size:10pt; font-weight:600;\">TO:</span></p><p><br/></p></body></html>"))
        self.label_4.setText(_translate("UnitConverter",
                                        "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">TYPE:</span></p></body></html>"))




if __name__ == "__main__":
    import sys


    app = QtWidgets.QApplication(sys.argv)
    UnitConverter = QtWidgets.QMainWindow()
    ui = Ui_UnitConverter()
    ui.setupUi(UnitConverter)
    UnitConverter.show()
    sys.exit(app.exec_())

CODE EXPLANATION:

Importing the modules which are to be required that, are Qtcore, Qtwidgets, and some more for the best extraction of the model, then introducing a function called unit converter with an object to create an instance with its dimensional size, central widget, vertical layout, shadow frames to get an attractive output. We add items to the content like seconds, milliseconds, minute, hour, day, and year, and adds widgets Combobox from self, drop shadow frame, and geometry with QtCore and QRect with some dimensions of 260,450,191,31 with a font style of Segoe UI. Setting layout direction from left to right with a background color, border, border radius, and text-aligned to the center. We use Html to and CSS to get the output clear and attractive with significant colors with the module.

OUTPUT:

Standard GUI Unit Converter using PyQt5 in Python

Related Topics

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

How to Iterate through a Dictionary in Python

In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example. Introduction to Dictionary in Python Dictionary is a special type of...

4 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.