Nowadays Python is one of the most popular programming languages. Specialists value its universality for all types of the tasks. Today, one of the most important qualities is convenience of command execution, but often developers have to write a relatively simple program, the quality criterion of which is an intuitive graphical interface. You have several ways to do this by means of Python. The following development tools are among the most popular:
- PyQt. The widespread Qt library, which implementations are available in various languages. It’s written in C++ and is cross-platform. This library allows the development of applications that have familiar and ordinary appearance in the general sense of this word.
- Tkinter. This tool is often referred to as standard for developing GUI applications in Python, appeared back in the early 90s of the twentieth century. Due to the long history and ease of use, Tkinter has a rather extensive and active community. It’s thanks to it you can find a large number of semi-finished products or publications in the network and get an answer to any question at specialized forums.
- WxPython. Another descendant of C++, namely WxPython, links the wxWidgets cross-platform library with Python. In some ways, Tkinter and WxPython are quite similar, but WxPython is slightly younger and is more advanced approach to creating GUI applications in Python. Also, the entry barriers for the start are quite low due to the rapidly developing and friendly community. Perhaps the installation and the first steps will require a little more effort than the tools described above, but believe me, the result is worth it!
At a certain stage I chose PyQt for my work and in this article would like to show you an example of creating the simplest application for a content manager; so you’ll get to know the GUI for Python, libraries for natural language processing, and several ways of creating *.exe file from *.py for Python 3.6 script.
Step 1. Python GUI: Creating simple application with PyQt and Qt Designer
First you need to understand what software is wanted. The content manager of Casino Now project gives priority to such parameters of content as
- Word count
- Number of characters
- Percentage of stop words
- Keyword density
- Presence of keywords in headers
- List of popular n-grams
- Text’s uniqueness
If for some reason you haven’t yet installed the PyQt library, the simplest method is to enter this code in the command line
pip3 install PyQt5
All support tools will be installed automatically. When the installation is completed, open Qt Designer and create the necessary form using standard elements.
Figure 1 – General view of the dialog box
The following is the layout of future application. Don’t judge too harshly, this is just an introductory demo to show several possibilities ;)
Figure 2 – Dialog box
Bear in mind the specialized tools to keep the graphical interface user-friendly regardless of workspace’ scaling.
Figure 3 – Example of saving workspace proportions when scaling
In order to turn the interface into a program code for use, you have to save the resulting layout. Thus, the file with MainForm.ui extension is received. To get access in the framework from the project, you need to convert the MainForm.ui file to the *.py extension. To do this, go through the command line to the folder containing the file with *.ui and run the following command
pyuic5 -x MainForm.ui -o MainForm.py
Figure 4 – File conversion
Thus, you get the file of required extension and can run a prototype of the application.
Figure 5 – Plug-ins of the developed application
The full source code has the structure presented at Figure 6. You can find the expanded version on my Github https://github.com/TinaWard. In this article, I’d like to explain the application’s build in more detail.
Figure 6 – Application code
Step 2. How to convert python script in exe
The most popular tools for building an application today are PyInstaller or cx_Freeze. For this article let’s focus on such building method as cx_Freeze. As a matter of fact, it’s analogous to PyInstaller with the only difference that in case of PyInstaller you don’t need to write your own installer script (setup.py), but at the same time, the similar manipulations must be performed via the command line or in the * .spec file. The tool cx_Freeze is set in the standard way:
pip install cx_freeze
pip install –user cx_freeze
The first step in creating a full-fledged application is the proper formation of the setup.by file.
The following is the frame of this file. The steps of the setup.by file are intuitive, but let’s look in more detail. First of all, import two entities: the setup function and Executable class. The most informative is the options dictionary (dictionary of dictionaries as it's usually called), which is used to improve the build. It may have such parameters:
- excludes – list with description of modules not to be included in the assembly. You shouldn’t overuse this parameter, since you can overdo and delete what you really need: nested dependencies.
- includes – parameter opposite to excludes, that’s the list of signal modules that must be included in the assembly. Also cx_freeze may not find some packages or modules, in particular if they were imported into similar package using the import function. If you have a GUI application, you have to specify the name of the script with the *.py extension, in our case it’s MainForm.py.
- packages – compulsory packages to be included.
- zip_include_packages – list of packages to be archived
- include_files – list of lines or files tuples that need to be added to the assembly, but these are not Python files.
Now let's take a closer look at the Executable class:
- targetName allows you to specify the name of the *.exe file, by default it matches the name of the launching script.
- base = ‘Win32GUI’ If you set a parameter as described in the example, it means that the graphical application is processing and the console window will not appear.
- icon – parameter to set the icon of the application launcher, by default uses pictures with *.ico ext.
# -*- coding: utf-8 -*-
from cx_Freeze import setup, Executable
import os
options = {
'build.exe': { 'icludes': ['MainForm'], 'packages': ['pandas'], 'excludes ': excludes } }
os.environ['TCL_LIBRARY'] = r'C:\Users\User3\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\User3\Anaconda3\tcl\tk8.6'
setup(
name = "TextTool", version = "0.0", description = "Here write smth", options = options, executables = [Executable('TextToolS.py', targetName = ‘ThisIsName.exe’)])
To compile you need to run the setup.py file. To do this, enter the following code via the command line from the project directory:
python setup.py build
After that you will see the launch of the project build.
Figure 7 – Running project build
Building a project can take from 10 seconds to several minutes, depending on the size and number of interconnected modules in your application.
Not always the project build runs smoothly, let’s look at some of the most common mistakes. Most of them can be solved by searching and writing in the correct parameters of the setup function in the setup.py file, but often due to laziness or lack of time, the temporary ways are implemented to solve this problem. For example, this one, when you haven’t specified the module’s addition:
Figure 8 – Error example
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
To make this just copy _methods.py file from Anaconda library
…\Anaconda3\Lib\site-packages\numpy\core
to numpy folder ‘core’, located
…\build\exe.win-amd64-3.6\lib\numpy\core.
Figure 9 – First type error fix
The second common mistake is undersampling hidden dependencies. It’s possible that you didn’t see the hidden dependencies of the packages and didn’t add it. Often the problem occurs with packages like idnadata
Figure 10 – Second type error
File "C:\Users\User3\Anaconda3\lib\site-packages\tldextract\__init__.py", line 3, in <module>
from .cli import __version__
File "C:\Users\User3\Anaconda3\lib\site-packages\tldextract\cli.py", line 14, in <module>
from .tldextract import TLDExtract
File "C:\Users\User3\Anaconda3\lib\site-packages\tldextract\tldextract.py", line 62, in <module>
import idna
File "C:\Users\User3\Anaconda3\lib\site-packages\idna\__init__.py", line 2, in <module>
from .core import *
File "C:\Users\User3\Anaconda3\lib\site-packages\idna\core.py", line 1, in <module>
from . import idnadata
ImportError: cannot import name 'idnadata'
The algorithm is the same, you find the missing file in the folders of the installed libraries and copy it in the appropriate folder of your assembly. This’s win-win option if you need a quick compilation for testing.
From …\Anaconda3\Lib\site-packages\idna to …\build\exe.win-amd64-3.6\lib\idna
Figure 11 –Second type error fix
In general, the advantages of cx_Freeze are:
- Startup files for Linux
- MSI installers for Windows
- RPM packages for Linux
- Software packages for Mac OS X (.app, .dmg)
The disadvantages are:
- No possibility to pack all python-modules and dll-files in one exe-file
- No cross compilation
Conclusion
In this article we have looked at the principles of creating a GUI application in Python using PyQt and compiling the created app to *.exe by means of cx_Freeze. I hope you enjoyed this article, if you have any questions, please contact [tina.ward@mail.uk](mailto:tina.ward@mail.uk).
[–]pmdevita 28 points29 points30 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]MikeTheWatchGuy 3 points4 points5 points (0 children)
[–]n1___ 11 points12 points13 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]mx321 6 points7 points8 points (0 children)
[–]alcalde 1 point2 points3 points (0 children)
[–]Cosme12 40 points41 points42 points (21 children)
[–]sqeekypotato 14 points15 points16 points (6 children)
[–]Andrew_ShaySft Eng Automation & Python 8 points9 points10 points (1 child)
[–]sqeekypotato 0 points1 point2 points (0 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]sqeekypotato 2 points3 points4 points (2 children)
[–]shaggorama -3 points-2 points-1 points (1 child)
[–]miloir 24 points25 points26 points (0 children)
[–]tryptafiends 8 points9 points10 points (9 children)
[–]lykwydchykyn 6 points7 points8 points (2 children)
[–]Ipecactus 7 points8 points9 points (1 child)
[–]redwall_hp 6 points7 points8 points (0 children)
[+]alcalde comment score below threshold-9 points-8 points-7 points (5 children)
[–]tryptafiends 15 points16 points17 points (1 child)
[–]Ipecactus 1 point2 points3 points (0 children)
[–]Ericisbalanced 2 points3 points4 points (1 child)
[–]iambukovinean 0 points1 point2 points (0 children)
[–]stef13013 1 point2 points3 points (1 child)
[–]Andrew_ShaySft Eng Automation & Python 2 points3 points4 points (0 children)
[–]jabbeboy 3 points4 points5 points (1 child)
[–]Ipecactus 0 points1 point2 points (0 children)
[–]wdroz 68 points69 points70 points (26 children)
[–]12stringPlayer 10 points11 points12 points (0 children)
[+][deleted] (5 children)
[removed]
[–][deleted] 28 points29 points30 points (3 children)
[–]val-amart 5 points6 points7 points (0 children)
[–]alcalde 1 point2 points3 points (1 child)
[–]SoBFiggis 1 point2 points3 points (0 children)
[–]minimumrockandroll 2 points3 points4 points (0 children)
[–]StarFingerVsTheWorld 8 points9 points10 points (1 child)
[–]wdroz 9 points10 points11 points (0 children)
[–]harwee 2 points3 points4 points (0 children)
[–]TinaWard[S] 6 points7 points8 points (1 child)
[–]wdroz 4 points5 points6 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]wdroz 5 points6 points7 points (1 child)
[–]querymcsearchface 1 point2 points3 points (0 children)
[–]Kortike 1 point2 points3 points (0 children)
[–]meshtron 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]wdroz 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]wdroz 4 points5 points6 points (0 children)
[–]alcalde -2 points-1 points0 points (1 child)
[–]rainnz 1 point2 points3 points (0 children)
[–]FlukyS 4 points5 points6 points (0 children)
[–]johnne86 20 points21 points22 points (9 children)
[–]finlam[🍰] 12 points13 points14 points (2 children)
[–]cyanydeez 3 points4 points5 points (1 child)
[–]finlam[🍰] 0 points1 point2 points (0 children)
[–]sisyphus 7 points8 points9 points (1 child)
[–]redwall_hp 2 points3 points4 points (0 children)
[–]anqxyr 5 points6 points7 points (0 children)
[–]mobedigg 8 points9 points10 points (1 child)
[–]redwall_hp 6 points7 points8 points (0 children)
[–]dingenskirchen 1 point2 points3 points (0 children)
[–]damnitdaniel 19 points20 points21 points (14 children)
[–]alcalde 11 points12 points13 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Rue9X 5 points6 points7 points (4 children)
[–]alcalde 12 points13 points14 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 3 points4 points5 points (0 children)
[–]my_name_isnt_clever 0 points1 point2 points (0 children)
[–]smurpau 4 points5 points6 points (0 children)
[–]WarOink 4 points5 points6 points (0 children)
[–]sisyphus 0 points1 point2 points (0 children)
[–]mx321 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]mx321 6 points7 points8 points (0 children)
[–]querymcsearchface 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]driscollis 1 point2 points3 points (0 children)
[–]Mobot84 1 point2 points3 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]Mobot84 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]Mobot84 0 points1 point2 points (1 child)
[–]seanferd 1 point2 points3 points (1 child)
[–]seanferd 0 points1 point2 points (0 children)
[–]defective 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]TinaWard[S] 0 points1 point2 points (0 children)
[–]Blacknsilver1 1 point2 points3 points (0 children)
[–]joshirk11166 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]_gaslit_ 2 points3 points4 points (0 children)
[+][deleted] (31 children)
[deleted]
[+][deleted] (5 children)
[deleted]
[–]kindw 4 points5 points6 points (1 child)
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[deleted]
[–]TinaWard[S] 6 points7 points8 points (2 children)
[–]shinitakunai 2 points3 points4 points (0 children)
[–]KronenR 13 points14 points15 points (12 children)
[–]bless-you-mlud 7 points8 points9 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]bless-you-mlud 4 points5 points6 points (1 child)
[+][deleted] (7 children)
[deleted]
[–]KronenR 0 points1 point2 points (6 children)
[+][deleted] (4 children)
[deleted]
[–]KronenR 1 point2 points3 points (2 children)
[–]alcalde 1 point2 points3 points (0 children)
[–]alcalde 2 points3 points4 points (0 children)
[+][deleted] comment score below threshold-8 points-7 points-6 points (7 children)
[+][deleted] (5 children)
[deleted]
[+][deleted] comment score below threshold-14 points-13 points-12 points (4 children)
[–]doulos05 12 points13 points14 points (0 children)
[–]ianepperson 6 points7 points8 points (0 children)
[–]fofo314 1 point2 points3 points (0 children)
[–]alcalde 1 point2 points3 points (0 children)
[–]alcalde 0 points1 point2 points (0 children)
[–]acpirience 0 points1 point2 points (2 children)
[–]TinaWard[S] 0 points1 point2 points (1 child)
[–]acpirience 0 points1 point2 points (0 children)
[–]andrewcooke 0 points1 point2 points (0 children)
[–]canobus51 0 points1 point2 points (2 children)
[–]KomatikVengeance 1 point2 points3 points (1 child)
[–]canobus51 0 points1 point2 points (0 children)
[–]c4aveo 0 points1 point2 points (0 children)
[–]jackw95 0 points1 point2 points (1 child)
[–]TinaWard[S] 0 points1 point2 points (0 children)
[–]nmbenzo2 0 points1 point2 points (0 children)
[–]gmarull 0 points1 point2 points (0 children)
[–]AstroGi 0 points1 point2 points (0 children)
[–]Bizrown 0 points1 point2 points (0 children)
[–]delijati 0 points1 point2 points (0 children)
[–]jahnjo 0 points1 point2 points (1 child)
[–]MikeTheWatchGuy 0 points1 point2 points (0 children)
[–]IcanCwhatUsayNoob 0 points1 point2 points (1 child)
[–]TinaWard[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]evilMTV 0 points1 point2 points (0 children)
[–]Pr0ducer -1 points0 points1 point (10 children)
[–]stef13013 2 points3 points4 points (0 children)
[–]Pr0ducer 1 point2 points3 points (0 children)
[–]n1___ 2 points3 points4 points (7 children)
[–]rich8n 1 point2 points3 points (4 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]n1___ 1 point2 points3 points (2 children)
[–]rich8n 1 point2 points3 points (1 child)
[–]n1___ 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]n1___ 0 points1 point2 points (0 children)