Here's how to create your first desktop application in python

Learning object oriented programming vs functional programming

Last updated on April 6th, 2024 at 08:13 pm

Most young developers have questions about creating desktop software using python. But before going into the process of developing a desktop application, they should learn python programming beforehand to learn concepts related to python.

Step By Step Guide to Create a GUI App in Python

Step 1

In this step, define the current task. Deciding what needs to be solved with the application explains further steps. The field has a variety of usage, for example, Data Visualizations, personal application performance to work with images, text, Business automation GUI's for managing tasks, and developing systems and monitoring.

best ai and ml coursesPrimary estimation of the functionality and size of the application is necessary as it will help choose the best-suited GUI tool kit.

In case you are not familiar with Graphical User Interface (GUI), it is recommended to take any of the available AI and machine learning courses to clear the fundamentals.  

Step 2   

Choose the correct GUI package and play around with it using python. There are multiple Python-based packages available to do this. One of the easiest ways to do so is by using Tkinter. It allows developers to create small and simple applications using a GUI interface. Popular third-party packages include PyQt, Kivy, WxPython, and Pyside. To know about these, individuals can look at the Python desktop application development tutorial.

Step 3

Here PyQt5 is used as a GUI toolkit for the desktop application. Next, download and install the package.

Step 4 

Then create a pyqt_app1.py file to import PyQt5 modules. After creating PyqtApp class, in the _init_function, in the bottom, create and import instruction with a file name with if _name+ == “_main”: and type lines with calling pyqt based app, importing sys module, calling show () to start the GUI application.

from PyQt5 import QtWidgets, QtGui, QtCore

class PyQtApp(QtWidgets.QWidget):

   

    def __init__(self, parent=None):

        QtWidgets.QWidget.__init__(self, parent)

        self.setWindowTitle("PyQt Application")

        self.setWindowIcon(QtGui.QIcon("Your/image/file.png"))

 

if __name__ == "__main__":

    import sys

    app = QtWidgets.QApplication(sys.argv)

    myapp = PyQtApp()

    myapp.show()

    sys.exit(app.exec_())

 

Step 5

Then add some style, font, and position of the application. Change the background colour by altering the line - self.element.setStyleSheet(“background-color: #hex number or rgba(). But to position the window, a desktop resolution is required. But this can be done by using multiple codes.

from PyQt5 import QtWidgets, QtGui, QtCore

class PyQtApp(QtWidgets.QWidget):

   

    def __init__(self, parent=None):

        QtWidgets.QWidget.__init__(self, parent)

        self.setWindowTitle("PyQt Application")

        self.setWindowIcon(QtGui.QIcon("Your/image/file.png"))

        self.setMinimumWidth(resolution.width() / 3)

        self.setMinimumHeight(resolution.height() / 1.5)

        self.setStyleSheet("QWidget {background-color:

                           rgba(0,41,59,255);}

                           QScrollBar:horizontal {

                           width: 1px; height: 1px;

                           background-color: rgba(0,41,59,255);}  

                           QScrollBar:vertical {width: 1px;

                           height: 1px;

                           background-color: rgba(0,41,59,255);}")

 

if __name__ == "__main__":

    import sys

    app = QtWidgets.QApplication(sys.argv)

    desktop = QtWidgets.QApplication.desktop()

    resolution = desktop.availableGeometry()

    myapp = PyQtApp()

    myapp.setWindowOpacity(0.95)

    myapp.show()

    myapp.move(resolution.center() - myapp.rect().center())

    sys.exit(app.exec_())

else:

    desktop = QtWidgets.QApplication.desktop()

    resolution = desktop.availableGeometry()

 

Step 6

In this step, adding functionality to the app is necessary. After all, while solving tasks, a graphical interface will make the user comfortable using the application. We can also add frames, fields, buttons and other graphics into the application. Using buttons and text fields will provide good and effective results. For best view buttons, here is how to create a new class for the application with styling and font.

from PyQt5 import QtWidgets, QtGui, QtCore

font_but = QtGui.QFont()

font_but.setFamily("Segoe UI Symbol")

font_but.setPointSize(10)

font_but.setWeight(95)

 

class PushBut1(QtWidgets.QPushButton):

   

    def __init__(self, parent=None):

        super(PushBut1, self).__init__(parent)

        self.setMouseTracking(True)

        self.setStyleSheet("margin: 1px; padding: 7px;

                           background-color: rgba(1,255,0,100);

                           color: rgba(1,140,0,100);

                           border-style: solid;

                           border-radius: 3px; border-width: 0.5px;

                           border-color: rgba(1,140,0,100);")

   

    def enterEvent(self, event):

        self.setStyleSheet("margin: 1px; padding: 7px;

                           background- color: rgba(1,140,040,100);

                           color: rgba(1,140,255,100);

                           border-style: solid; border-radius: 3px;

                           border-width: 0.5px;

                           border-color: rgba(1,140,140,100);")

   

    def leaveEvent(self, event):

        self.setStyleSheet("margin: 1px; padding: 7px;

                           background-color: rgba(1,255,0,100);

                           color: rgba(1,140,0,100);

                           border-style: solid;

                           border-radius: 3px; border-width: 0.5px;

                           border-color: rgba(1,140,0,100);")

class PyQtApp(QtWidgets.QWidget):

   

    def __init__(self, parent=None):

        QtWidgets.QWidget.__init__(self, parent)

        self.setWindowTitle("PyQt Application")

        self.setWindowIcon(QtGui.QIcon("Your/image/file.png"))

        self.setMinimumWidth(resolution.width() / 3)

        self.setMinimumHeight(resolution.height() / 1.5)

        self.setStyleSheet("QWidget

                           {background-color: rgba(1,255,0,100);}

                           QScrollBar:horizontal

                           {width: 1px; height: 1px;

                           background-color: rgba(0,140,0,255);}

                           QScrollBar:vertical

                           {width: 1px; height: 1px;

                           background-color: rgba(0,140,0,255);}")

        self.textf = QtWidgets.QTextEdit(self)

        self.textf.setPlaceholderText("Results...")

        self.textf.setStyleSheet("margin: 1px; padding: 7px;

                                 background-color:      

                                 rgba(1,255,0,100);

                                 color: rgba(1,140,0,100);

                                 border-style: solid;

                                 border-radius: 3px;

                                 border-width: 0.5px;

                                 border-color: rgba(1,140,0,100);")

        self.but1 = PushBut1(self)

        self.but1.setText("")

        self.but1.setFixedWidth(72)

        self.but1.setFont(font_but)

        self.but2 = PushBut1(self)

        self.but2.setText("")

        self.but2.setFixedWidth(72)

        self.but2.setFont(font_but)

        self.but3 = PushBut1(self)

        self.but3.setText("")

        self.but3.setFixedWidth(72)

        self.but3.setFont(font_but)

        self.but4 = PushBut1(self)

        self.but4.setText("")

        self.but4.setFixedWidth(72)

        self.but4.setFont(font_but)

        self.but5 = PushBut1(self)

        self.but5.setText("")

        self.but5.setFixedWidth(72)

        self.but5.setFont(font_but)

        self.but6 = PushBut1(self)

        self.but6.setText("")

        self.but6.setFixedWidth(72)

        self.but6.setFont(font_but)

        self.but7 = PushBut1(self)

        self.but7.setText("")

        self.but7.setFixedWidth(72)

        self.but7.setFont(font_but)

        self.grid1 = QtWidgets.QGridLayout()

        self.grid1.addWidget(self.textf, 0, 0, 14, 13)

        self.grid1.addWidget(self.but1, 0, 14, 1, 1)

        self.grid1.addWidget(self.but2, 1, 14, 1, 1)

        self.grid1.addWidget(self.but3, 2, 14, 1, 1)

        self.grid1.addWidget(self.but4, 3, 14, 1, 1)

        self.grid1.addWidget(self.but5, 4, 14, 1, 1)

        self.grid1.addWidget(self.but6, 5, 14, 1, 1)

        self.grid1.addWidget(self.but7, 6, 14, 1, 1)

        self.grid1.setContentsMargins(7, 7, 7, 7)

        self.setLayout(self.grid1)

 

if __name__ == "__main__":

    import sys

    app = QtWidgets.QApplication(sys.argv)

    desktop = QtWidgets.QApplication.desktop()

    resolution = desktop.availableGeometry()

    myapp = PyQtApp()

    myapp.setWindowOpacity(0.95)

    myapp.show()

    myapp.move(resolution.center() - myapp.rect().center())

    sys.exit(app.exec_())

else:

    desktop = QtWidgets.QApplication.desktop()

    resolution = desktop.availableGeometry()

Step 7

You can add a few more fields and explore the possibilities of PyQt.

Step 8

Then connect the buttons and functions for a calling event. For this, we need to add an additional line to the  _init_ function of the main class.

self.but1.clicked.connect(self.on_but1)

Step 9

You can add images in this step. The second button in the image will call the image file from the text to put it in the right bottom corner. 

Adding QLabel: 

self.lb1 = QtWidgets.QLabel(self)

self.lb1.setFixedWidth(72)

self.lb1.setFixedHeight(72)

 

Adding function:

def on_but2(self):

    txt = self.textf.toPlainText()

    try:

        img = QtGui.QPixmap(txt)

        self.lb1.setPixmap(img.scaledToWidth(72,

                           QtCore.Qt.SmoothTransformation))

    except:

        pass

To connect the second button and the function: 

self.but2.clicked.connect(self.on_but2)

Step 10

Complete and run the application. Apart from this PyQt has various other applications, and you can use those to create complete desktop applications.

 If you are not familiar with coding, you can learn python programming or enroll in any AI and machine learning courses. Apart from this, you can also look at the Python desktop application development tutorial

Level 1
Copyscape Premium Verification Text Clear, But Code Matched
Grammarly Premium Score 96
Readability Score 12.1
Primary Keyword Usage Done
Secondary Keyword Usage Done
Highest Word Density  Self – 7.39% 
Data/Statistics Validation Date
Level 2
YOAST SEO Plugin Analysis 3 Green, 4 Red
Call-to-action Tone Integration NA
LSI Keyword Usage NA
Level 3
Google Featured Snippet Optimization NA
Content Camouflaging NA
Voice Search Optimization NA
Generic Text Filtration Done
Content Shelf-life

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Our Programs

Do You Want To Boost Your Career?

drop us a message and keep in touch