Home » Python

Print ‘Hello World’ using Python and PyQT -2 (Practical)

Print ‘Hello World’ using Python and PyQT -2 (Practical): This article will cover the practical implementation of those terms we studied in previous articles (Print ‘Hello World’ using Python and PyQT (first PyQT program) and Print ‘Hello World’ using Python and PyQT -2 (Theory)). Here, we will learn the second method to create and run GUI apps, say a simpler method. And, believe my words whether you love codes or not, but you would certainly love this one. We will create the same "Hello World" layout using Qtdesigner, a drag and drop way. Like you are always welcome to do so, feel free to ask any query in comment section.
Submitted by Atul Anand, on December 04, 2017

The first GUI program (QtDesigner)

Since, I have promised to teach you a simpler method; I am stating it as simple as possible. Let’s break it into few steps:

  1. Design your GUI with drag and drop tools in Qtdesigner.
  2. Save it with '.ui' extension.
  3. Convert it into executable python file.
  4. Run the python file.

Now, I am elaborating these steps to make you go comfortable.

Step 1:

• Open Qtdesigner.

• Select widget and click on create.

Qtdesigner - widget

• Select and drag and drop "Label" in the widget area.

Qtdesigner - label drag and drop

• Write "Hello World!" You are done with this step. Either go to next step or follow this procedure below if you want to customize it and look attractive:

  • Resize the layout by pointing at its exteriors or corners and shifting mouse pointer.
  • Go to property editor on the right side of IDLE:
    • Change Font size and style by clicking on "value" part of the "font" Property. (See, below figure -1)
    • Change alignment of text by scrolling down to LABEL field, and clicking on "value" part of "alignment" Property. (See, below figure -2)
Qtdesigner - font settings

Figure -1

Qtdesigner - alignment settings

Figure -2

Step 2:

Save it on your desired location in your project folder. Ensure that its extension is ".ui".

Note: This is the content of the xml file for whatever we have designed yet

XML file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <widget class="QWidget" name="PrintHELLO">
  <property name="windowModality">
  <property name="geometry">
  <property name="font">
    <family>Segoe Script</family>
  <property name="windowTitle">
  <layout class="QFormLayout" name="formLayout_2">
   <property name="fieldGrowthPolicy">
   <item row="0" column="1">
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Hello World!</string>
     <property name="alignment">

Step 3

Convert xml file to executable python file. Follow the steps provided in the pyuic4 Section.

Note: I am providing here the source code for python file. This is just to make you familiar with the body of the code. You don’t need to bother about the code content at this phase. As we, will do some modifications in the later stage , if required. This is the content of the python file for whatever we have designed yet

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'NEW.ui'
# Created by: PyQt4 UI code generator 4.11.4
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
class Ui_PrintHELLO(object):
    def setupUi(self, PrintHELLO):
        PrintHELLO.resize(259, 62)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Segoe Script"))
        self.formLayout_2 = QtGui.QFormLayout(PrintHELLO)
        self.label = QtGui.QLabel(PrintHELLO)
        self.formLayout_2.setWidget(0, QtGui.QFormLayout.FieldRole, self.label)


    def retranslateUi(self, PrintHELLO):
        PrintHELLO.setWindowTitle(_translate("PrintHELLO", "printHELLO", None))
        self.label.setText(_translate("PrintHELLO", "Hello World!", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    PrintHELLO = QtGui.QWidget()
    ui = Ui_PrintHELLO()

Step 4

Run the python file. Yup, we are done now.


output of python & pyqt hello world program


This way, we have printed “Hello World!" as a more customized outcome. And, mind bobbling complexities with the code was also eradicated. Yes, after this you have reached to an equivocal situation. Its only you who is going to decide which method suits you. I will discuss both methods whenever needed. But I will prefer drag and drop method as my favorite one, since it provides me with speed. But, you don’t worry. Whenever you want to ask for any doubt put your query in the comment section. Catch you later in next article. HAPPY LEARNING!

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.