<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged pyinstaller</title>
<link>https://coderlegion.com/tag/pyinstaller</link>
<description>Powered by Question2Answer</description>
<item>
<title>Convert Tkinter Python App to .Exe File [pyinstaller] Step by Step Guide</title>
<link>https://coderlegion.com/209/convert-tkinter-python-app-to-exe-file-pyinstaller-step-by-step-guide</link>
<description>&lt;p&gt;In this article, you will learn how to distribute a &lt;strong&gt;Tkinter&lt;/strong&gt; project an as &lt;strong&gt;.exe&lt;/strong&gt; setup file using &lt;strong&gt;PyInstaller&lt;/strong&gt;. This converts Python scripts into &lt;strong&gt;standalone executables&lt;/strong&gt; and makes it easy for people to install and use your applications. Users don't need to worry about installing Python or managing dependencies. This way, your programs can be easily moved between different computers and run securely. Plus, it gives your projects a more polished and professional look.&lt;/p&gt;
&lt;h2 id=&quot;environment-setup&quot;&gt;Environment Setup&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Python&lt;/strong&gt;: Ensure that Python is installed on your system, which includes Tkinter by default. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;For Windows&lt;/strong&gt;: Download and install Python from the &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.python.org/downloads&quot;&gt;official website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For Ubuntu&lt;/strong&gt;: You can download Python using the terminal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  sudo apt update
  sudo apt install python3
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For MacOS&lt;/strong&gt;: You can download Python using Homebrew:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  brew install python3
  Confirm the installation by typing `python3 --version` in the terminal.
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PyInstaller&lt;/strong&gt;: It is necessary for converting Python scripts into executable files. Launch your terminal or command prompt and execute the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pip install pyinstaller
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IDE or Text Editor&lt;/strong&gt;: Choose a preferred text editor or IDE like Visual Studio Code for coding.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tkinter&lt;/strong&gt;: Basic understanding of Tkinter, including widgets and event handling, is beneficial. Refer to the &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/tkinter.html&quot;&gt;Tkinter documentation&lt;/a&gt; or &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/190/introduction-to-tkinter-library-in-python&quot;&gt;this article&lt;/a&gt; for learning.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;div-blue&quot;&gt; &lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt; &lt;span class=&quot;alert-body&quot;&gt; This project expects familiarity with basic Python concepts.&lt;/span&gt; &lt;/div&gt;
&lt;h2 id=&quot;developing-a-tkinter-project&quot;&gt;Developing a Tkinter Project&lt;/h2&gt;
&lt;p&gt;Do check out &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/tk.html&quot;&gt;the documentation&lt;/a&gt; to learn more about GUI using Tkinter.&lt;/p&gt;
&lt;p&gt;We will create a simple calculator application using Tkinter in Python. It allows you to input two numbers and select an operation (addition, subtraction, multiplication, or division) using radio buttons. Upon clicking the &quot;Calculate&quot; button, the program performs the selected operation on the input numbers and displays the result in a modeless dialog box.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import tkinter as tk
from tkinter import messagebox

def  calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
if operation.get() == 1:     # Add
result = num1 + num2
elif operation.get() == 2:     # Subtract
result = num1 - num2
elif operation.get() == 3:     # Multiply
result = num1 * num2
elif operation.get() == 4:     # Divide
if num2 == 0:
messagebox.showerror(&quot;Error&quot;, &quot;Cannot divide by zero&quot;)
return
result = num1 / num2
result_window = tk.Toplevel(window)     # Modeless dialog box
result_window.geometry(&quot;200x100&quot;)
result_window.title(&quot;Result&quot;)
result_label = tk.Label(result_window, text=f&quot;The result is: {result}&quot;)     # Label widget
result_label.pack()

window = tk.Tk()     # Main window
window.geometry(&quot;457x335&quot;)
window.title(&quot;Calculator&quot;)

frame_numbers = tk.Frame(window)     # Frame
frame_numbers.pack()
label1 = tk.Label(frame_numbers, text=&quot;Number 1:&quot;)     # Labels and Entry widgets
label1.grid(row=0, column=0)
entry1 = tk.Entry(frame_numbers)
entry1.grid(row=0, column=1)
label2 = tk.Label(frame_numbers, text=&quot;Number 2:&quot;)
label2.grid(row=1, column=0)
entry2 = tk.Entry(frame_numbers)
entry2.grid(row=1, column=1)
frame_operations = tk.Frame(window)     # Frame
frame_operations.pack()
operation = tk.IntVar()     # Radiobuttons
addition = tk.Radiobutton(frame_operations, text=&quot;Addition&quot;, variable=operation, value=1)
addition.grid(row=0, column=0, sticky=&quot;w&quot;)
subtraction = tk.Radiobutton(frame_operations, text=&quot;Subtraction&quot;, variable=operation, value=2)
subtraction.grid(row=1, column=0, sticky=&quot;w&quot;)
multiplication = tk.Radiobutton(frame_operations, text=&quot;Multiplication&quot;, variable=operation, value=3)
multiplication.grid(row=2, column=0, sticky=&quot;w&quot;)
division = tk.Radiobutton(frame_operations, text=&quot;Division&quot;, variable=operation, value=4)
division.grid(row=3, column=0, sticky=&quot;w&quot;)
error_label = tk.Label(window, fg=&quot;red&quot;)     # Error label
error_label.pack()
calculate_button = tk.Button(window, text=&quot;Calculate&quot;, command=calculate)     # Button
calculate_button.pack()
window.mainloop()
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Firstly, import the Tkinter module and create a main window using &lt;code&gt;tk.Tk()&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Two frames, &lt;code&gt;frame_numbers&lt;/code&gt; and &lt;code&gt;frame_operations&lt;/code&gt;, are created to organize the layout of the widgets.&lt;/li&gt;
&lt;li&gt;Labels and Entry widgets are placed inside &lt;code&gt;frame_numbers&lt;/code&gt; to allow users to input two numbers.&lt;/li&gt;
&lt;li&gt;Radiobuttons, &lt;code&gt;addition&lt;/code&gt;, &lt;code&gt;subtraction&lt;/code&gt;, &lt;code&gt;multiplication&lt;/code&gt;, and &lt;code&gt;division&lt;/code&gt;, are placed inside &lt;code&gt;frame_operations&lt;/code&gt; to select the desired operation.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;messagebox&lt;/code&gt; is created to display error messages, such as division by zero.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;calculate()&lt;/code&gt; function is defined to perform the calculation based on user input. It retrieves the input numbers and selected operation, calculates the result, and creates a modeless dialog box &lt;code&gt;result_window&lt;/code&gt; using &lt;code&gt;tk.Toplevel()&lt;/code&gt; to display the result.&lt;/li&gt;
&lt;li&gt;Finally, the main event loop &lt;code&gt;window.mainloop()&lt;/code&gt; is started to run the application.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=11940241863597782013&quot; alt=&quot;pyinstl_calculator&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt; &lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt; &lt;span class=&quot;alert-body&quot;&gt; It is best to encapsulate functions and shared variables within a class.&lt;/span&gt; &lt;/div&gt;
&lt;h2 id=&quot;installing-pyinstaller&quot;&gt;Installing PyInstaller&lt;/h2&gt;
&lt;p&gt;To install PyInstaller using pip, follow these steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open your terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Type the following command and press Enter:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pip install pyinstaller
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the installation is complete, you can verify that PyInstaller has been installed correctly by running:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pyinstaller --version
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This command will display the version of PyInstaller installed on your system.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here's how PyInstaller converts python scripts to executable files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PyInstaller analyzes Python scripts and their dependencies.&lt;/li&gt;
&lt;li&gt;It bundles all dependencies, including the Python interpreter, into a single package.&lt;/li&gt;
&lt;li&gt;It creates standalone executables that can run independently on compatible systems.&lt;/li&gt;
&lt;li&gt;The tool optimizes and compresses the executable for efficient distribution.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;div-blue&quot;&gt; &lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt; &lt;span class=&quot;alert-body&quot;&gt; PyInstaller is cross-platform, supporting Windows, macOS, and Linux.&lt;/span&gt; &lt;/div&gt;
&lt;h2 id=&quot;converting-tkinter-project-to-exe-file&quot;&gt;Converting Tkinter Project to .exe File&lt;/h2&gt;
&lt;p&gt;Let's convert our calculator script to a .exe file.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open a command prompt or terminal window.&lt;/li&gt;
&lt;li&gt;Navigate to the directory containing your Python script.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pyinstaller --onefile --windowed calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;--onefile&lt;/code&gt; bundles everything into a single executable and &lt;code&gt;--windowed&lt;/code&gt; creates a GUI application without a console window.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PyInstaller will analyze your script and create a &lt;code&gt;dist&lt;/code&gt; directory in the same location, containing the converted .exe file (calculator.exe).&lt;/li&gt;
&lt;li&gt;Once the process is complete, you can find the &lt;code&gt;calculator.exe&lt;/code&gt; file in the &lt;code&gt;dist&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=10409504070909312298&quot; alt=&quot;pyinstl_run&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=16231717955459463347&quot; alt=&quot;pyinstl_dist&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-red&quot;&gt; &lt;span class=&quot;alert-header&quot;&gt;Caution:&lt;/span&gt; &lt;span class=&quot;alert-body&quot;&gt; Ensure that the Tkinter module has been installed.&lt;/span&gt; &lt;/div&gt;
&lt;p&gt;Possible Errors that you may encounter:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ModuleNotFoundError&lt;/strong&gt;: To resolve PyInstaller's inability to locate some imported modules or dependencies, ensure all required modules are installed and accessible. You can install missing modules using pip.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large Executable Size&lt;/strong&gt;: Optimize PyInstaller options to reduce .exe size. Use &lt;code&gt;--onefile&lt;/code&gt; for a single executable or &lt;code&gt;--exclude-module&lt;/code&gt; to exclude modules.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PermissionError&lt;/strong&gt;: Ensure you have write permissions for the output directory to avoid permission errors during PyInstaller execution.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Anti-virus False Positives&lt;/strong&gt;: Anti-virus programs may flag the .exe file as suspicious due to file bundling. To fix, whitelist the .exe or sign it with a digital signature.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;customizing-the-exe-setup-file&quot;&gt;Customizing the .exe Setup File&lt;/h2&gt;
&lt;p&gt;To customize the .exe file generated by PyInstaller, you can add a custom icon, metadata using specific options, window size and title of the .exe file.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Icon&lt;/strong&gt;: Utilize the &lt;code&gt;--icon&lt;/code&gt; option followed by the path to your icon file (.ico). This sets the icon for the executable.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pyinstaller --onefile --icon=calculator_icon.ico calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;: Craft a version file (.rc or .ini) containing metadata like file description, product name, and version information. Then, use the &lt;code&gt;--version-file&lt;/code&gt; option to specify the path to this file.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pyinstaller --onefile --version-file=calculator_version.rc calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Example of calculator_version.rc:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  1 VERSIONINFO
  FILEVERSION 1,0,0,0
  PRODUCTVERSION 1,0,0,0
  FILEDESCRIPTION &quot;Calculator Application&quot;
  PRODUCTNAME &quot;Calculator&quot;
  ICON &quot;calculator_icon.ico&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Window Size&lt;/strong&gt;: Use &lt;code&gt;--windowed&lt;/code&gt; to create a GUI application without a console window. &lt;code&gt;--noconsole&lt;/code&gt; can also be utilized, but it simply hides the console window and there might be a brief appearance of it. You can also set the window size in your Tkinter code.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Title of the .exe file&lt;/strong&gt;: Specify the executable name with &lt;code&gt;--name&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  pyinstaller --onefile --name=SimpleCalculator calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;testing-the-exe-setup-file&quot;&gt;Testing the .exe Setup File&lt;/h2&gt;
&lt;p&gt;We'll now test our executable file. I've converted the Python script to a .exe file using the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pyinstaller --onefile --windowed --name=SimpleCalculator --icon=calculator_icon.ico calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;SimpleCalculator.exe&lt;/code&gt; file can be found in the &lt;code&gt;dist&lt;/code&gt; folder.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15703158193762044820&quot; alt=&quot;pyinstl_execfile1&quot;&gt;&lt;/p&gt;
&lt;p&gt;Upon running the file, it works as intended.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=8593516799524454024&quot; alt=&quot;pyinstl_execfile2&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=1383139374120414111&quot; alt=&quot;pyinstl_execfile3&quot;&gt;&lt;/p&gt;
&lt;p&gt;To ensure compatibility, the file can be run on other systems as well.&lt;/p&gt;
&lt;p&gt;The following flowchart provides a comprehensive overview of the entire process from start to finish:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15510702019602131858&quot; alt=&quot;pyinstl_flowchart&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;distributing-your-tkinter-application&quot;&gt;Distributing Your Tkinter Application&lt;/h2&gt;
&lt;p&gt;To include multiple files while creating an executable using PyInstaller, you can use the &lt;code&gt;--add-data&lt;/code&gt; option.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pyinstaller --onefile --add-data &quot;file1.extension;.&quot; --add-data &quot;file2.extension;.&quot; calculator.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;--add-data&lt;/code&gt; option allows you to specify the path to the extra file or folder you wish to include. Use a semicolon &lt;code&gt;;&lt;/code&gt; to distinguish the source path from the destination directory inside the executable. A dot &lt;code&gt;.&lt;/code&gt; indicates that the file should be copied to the same directory as the executable.&lt;/p&gt;
&lt;p&gt;It can be used to include .csv files or SQLite databases. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pyinstaller --onefile --add-data &quot;todo_data.db;.&quot; todo_list.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=8817162348252474682&quot; alt=&quot;pyinstl_add_run&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=603164746969197611&quot; alt=&quot;pyinstl_add_dist&quot;&gt;&lt;/p&gt;
&lt;p&gt;You have various options for distributing your .exe file to users:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Direct Downloads&lt;/strong&gt;: You can host the .exe file on your website or server and offer users a direct link to download it. This method gives you complete control over distribution.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GitHub Releases&lt;/strong&gt;: If your project is on GitHub, you can utilize the Releases feature to distribute your .exe file. Upload the file as an asset when creating a new release, and users can download it from the Releases page.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Third-Party Platforms&lt;/strong&gt;: Several third-party platforms allow file hosting and distribution, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Google Drive&lt;/strong&gt;: Upload the .exe file to Google Drive and share the download link.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dropbox&lt;/strong&gt;: Similar to Google Drive, you can upload the .exe file to Dropbox and share the link.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OneDrive&lt;/strong&gt;: Microsoft's OneDrive offers file hosting and sharing capabilities.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can download the executable file we made in this article by clicking &lt;a rel=&quot;nofollow&quot; href=&quot;[14]&quot; download=&quot;SimpleCalculator.exe&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In conclusion, creating a calculator application that can add, subtract, multiply and divide, using &lt;strong&gt;Tkinter&lt;/strong&gt; and converting it into a &lt;strong&gt;.exe file&lt;/strong&gt; using &lt;strong&gt;PyInstaller&lt;/strong&gt; is a straightforward process that opens up numerous opportunities for &lt;strong&gt;software distribution&lt;/strong&gt;. By following the steps outlined in this article, you've learned how to harness the power of Tkinter to build intuitive graphical user interfaces and leverage PyInstaller to package your Python scripts into &lt;strong&gt;standalone executables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now that you've successfully created your calculator application and transformed it into a .exe file, it's time to share your creation with the world. Distributing your .exe files allows others to easily access and use your software, whether it's for personal or professional use. Whether you choose to host your files on your own website, share them via GitHub releases, or utilize third-party platforms, the possibilities for distribution are endless.&lt;/p&gt;
</description>
<category>Project Dev</category>
<guid isPermaLink="true">https://coderlegion.com/209/convert-tkinter-python-app-to-exe-file-pyinstaller-step-by-step-guide</guid>
<pubDate>Mon, 01 Apr 2024 06:11:39 +0000</pubDate>
</item>
</channel>
</rss>