Write a short code
-
Create a folder myapp
-
Create a file in myapp directory named main.py (You could name it anything other than main.py).
-
Open main.py and paste the following code.
import tkinter as Tkinter
def main():
top = Tkinter.Tk()
top.geometry("500x500")
B = Tkinter.Button(top, text ="Ghanshyam Maharaj")
B.pack()
top.mainloop()
if __name__ == '__main__':
main()
- Open terminal in myapp directory and type
python3 -m venv env
NOTE: python3 is necessary, it will not work on python2. So take care of this thing.
source env/bin/activate
If you see (env)
, it indicataes virtual environment is activated succesfully.
python3 main.py
This will display a small GUI application that says, Ghanshyam Maharaj
(It does not matter if you write python3 main.py
or python main.py
. It will use the python version which you used while creating a virtual environment)
Use the package manager pip to install cx_Freeze.
pip install cx_Freeze
Check here if cx_freeze is compatible with the python version you are using else it will throw some errors.
You could name it anything. Copy the following code.
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
# build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
build_exe_options = {"packages": ["os"], "excludes": []}
setup(
name="Ghanshyam",
url='<url to your github code>', # Not necessary
version="1.0.0", # [Learn how to specify verion no. at https://semver.org/
description="My GUI application which displays Ghanshyam Maharaj",
options={
# https://cx-freeze.readthedocs.io/en/latest/distutils.html#build-exe
"build_exe": build_exe_options,
},
executables=[
Executable("main.py")
]
)
python setup.py build
It might give an error such as
ValueError: Cannot find required utility
patchelf
in PATH
sudo apt install patchelf
and again run python setup.py build
It will take a while and after the process is completed you will see a folder named build in your directory where you will have a folder named exe.linux-<architecture>-<python-verion>
Open that folder and you will find main
Now, you can share this build folder with anyone and they can use this script when they will execute main
To execute main open terminal and type ./main
You can turn off Virtual Environment if you wish.
Now to build a folder and you will have a file that will look something like this.
exe.linux-x86_64-3.8
NOTE: We will be using this name(exe.linux-x86_64-3.8) throughout this page. It might differ in your computer.
cd exe.linux-x86_64-3.8
mkdir usr
mv * usr/
mkdir DEBIAN
cd DEBIAN/
Create a new file named control
vi control
Paste the following code
Package: Ghanshyam
Version: 1.0.0
Architecture: all
Essential: no
Priority: optional
Depends: less, parted, rsync
Maintainer: Ghanshyam Maharaj
Description: Harikrushna Maharaj, Jai Swaminarayan.
Keywords: ghanshyam;swaminarayan;
Preinst is a bash script that will run before installation. It is usually used to check if any configuration files need to be removed or not. And path to older icons are removed. If old icons are kept, new icons are sometimes not replaced. You can view in this video and skip to 18:12
vi preinst
#!/bin/bash
echo "Jai Swaminarayan, preinst script"
chmod 755 preinst
cd ../usr/
mkdir bin/
mkdir bin/ghanshyam
mv * bin/ghanshyam/
Replace <ghanshyam> with appname. Write <appname> in smallcase. Coz all apps in bin folder are lowercase.
mkdir share/
cd share/
mkdir icons
mkdir icons/ghanshyam
cd icons/ghanshyam/
Replace <ghanshyam> with appname (Paste the icon file in this directory)
PNG format is also supported. .ico file is Not mandatory.
cd ../..
mkdir applications
cd applications
vi ghanshyam.desktop
(No need to create ghanshyam folder here, every app name is unique)
Paste the following code
[Desktop Entry]
Version=1.0
Name=ghanshyam
Comment=Comment in ghanshyam.desktop file
Exec=/usr/bin/ghanshyam/main
Icon=/usr/share/icons/ghanshyam/icon.png
Terminal=false
Type=Application
StartupNotify=true
Categories=Utility;Application;
Replace
Exec=/usr/bin/ghanshyam/main
with your main application file
Replace
Icon=/usr/share/ghanshyam/icons/icon.png
with your icon directory path
Turn
Terminal=true
if you want to display terminal while your application is running.
NOTE: "exe.linux-x86_64-3.8" will be considered as root directory while creating Debian installer. Thus </> is included before usr
directory
One last thing to add is that by setting executable rights to your .desktop file, it automatically takes the specified Icon and Name (specified in the corresponding fields), as it should be. Be careful though, the filename doesn't really change, it still remains 'launcher_name_here.desktop' and not 'Name_field_here', the system chooses to display it like 'Name_field_here' because it's nicer without the .desktop extension.
Source: Ubuntu Docs
chmod +x ghanshyam.desktop
dpkg-deb --build exe.linux-x86_64-3.8/
dpkg-deb: building package 'ghanshyam' in 'exe.linux-x86_64-3.8.deb'.
ls
exe.linux-x86_64-3.8 exe.linux-x86_64-3.8.deb
If you are confused how your tree might look, here is a glance.
sudo dpkg -i exe.linux-x86_64-3.8.deb
Now you can search for ghanshyam in launcher and your new application is also added to favourites.
If the installer throws Architecture Error. You might need to replace <Architecture: all> with <Architecture: amd64> as per system.