C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Python

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

Printing ‘Hello World’ on GUI-2 (Theory): This article brings you one step further in the journey of learning GUI with PyQT. This article will give you knowledge of pre-essentials before you start with a new fun. Please don’t skip this article or you may face trouble somewhere in next articles. Like you are always welcome to do so, feel free to ask any query in comment section.
Submitted by Atul Anand, on December 02, 2017

Introduction

We can create a GUI with PyQT in two ways:

  1. Purely Code (refer to previous article if you have skipped that one.)
  2. Drag and drop with QT-designer.

We are going to study the second method in detail in this article. Before we take a leap, let’s first get familiar with some technical stuff which will make our journey more smooth and learning. Here we go:

Qtdesigner

This is an IDLE that provides a drag and drop feature. To dig it into details, refer to my first ever article on GUI Development using Python and PyQT. It comes with the PyQT package. So, you don’t need to bother anymore. You can easily find its application by searching from your desktop’s Search Tab or by searching on this directory path: 'C:\Python34\Lib\site-packages\PyQt4\designer.exe'.

Note: ‘C:’ drive is the installation path for my Python package (Python 34 denotes Python 3.4 version). Since, you are going to use it quite often; I would suggest you to create a shortcut on your homepage.

Get yourself very familiar with tools provided by Qtdesigner. The tools are pretty simple and easy to use. You will get to know about possible modifications in their functionalities as we go further in upcoming articles. We are actually working with the "xml" format of a file. And the file gets saved with a ".ui" extension, probably symbolizing User Interface.

pyuic4

Pay attention, all of the alphabets are in lowercase. This is a batch command used with other parameters to convert an "xml" file to an executable 'python' file in the CMD prompt. This is actually a batch file, named 'pyuic4.bat'. This is located at same location where Qtdesigner was present.

Don’t forget to check whether its location is added in your ENVIRONMENT VARIABLE’s PATH; if not ADD it there (Otherwise you will need to provide whole path of this batch file when using it in CMD prompt). You can check its file content in Notepad. This is the content of this batch file: '@"C:\Python34\python" "C:\Python34\Lib\site-packages\PyQt4\uic\pyuic.py" %1 %2 %3 %4 %5 %6 %7 %8 %9'

Note: You don’t need to dig deeper into this batch file. I am providing its content here in case you install a wrong version by mistake; then sometimes modification in this file can help you somehow. But, it’s better to go with a correct version.

P.S.: I am focusing on it because I was stuck with this stuff for 3 months or may be longer and nobody was there to help me out. Not even Google or YouTube; well that’s my personal experience of that time. Now enough resources are available. You can also visit my YouTube channel “U B Vzard” for its video guide and my PyQT journey. :)

Syntax of using this command

pyuic4 –x input.ui –o output.py

Note: input and output file can be of same name or different, depends on user. '-x' symbolizes for input file and '-o' symbolizes for output file.

Open command prompt where your 'xml' file was present, otherwise you need to provide complete path of your input and output file.

PyQT Hello World printing example

Special tip:

If you want to skip this CMD stuff, I can offer you another way. Make your own batch file that will perform the same task for you.

Steps:

  1. Open Notepad
  2. Paste this content there:
    @echo off
    call pyuic4 -x input.ui -o output.py
  3. Save it as : 'run.bat'
  4. Just put this file at same location of your ‘ui’ file. Make correction in input and output file as per you requirement.
  5. Run this batch file by double clicking on it. Hurray, you have got your python file ready with you. :)

Summary

After reading this article, you are quite familiar with the technologies and commands used in upcoming articles. Qtdesigner makes your work quite simpler and faster. You just need to do very little modifications the python code at the end, and get yourself a very much personal and interesting project. While, understanding the theories regarding pyuic4 batch file, you will find yourself paced and out of troubles. And you can also customize it whenever you get stuck in case of compatibility issue. So, now you are all set to move further to next article and do the actual work. See you there, HAPPY LEARNING!









COMMENTS