×

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

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 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.

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Isreal() 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...

4 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

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.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.