2

could someone please tell me why I am getting this error after converting my Python file into an executable?

The Error :

Traceback (most recent call last):
  File "MD2HTML.py", line 11, in <module>
  File "tkinter\__init__.py", line 2109, in iconbitmap
_tkinter.TclError: bitmap "icon.ico" not defined
[5572] Failed to execute script 'MD2HTML' due to unhandled exception!

[process exited with code 1]

Pyinstaller command: pyinstaller --onefile --icon=icon.ico MD2HTML.py

My code:

from tkinter import messagebox
import markdown
from tkinter import *

def go():
    result = markdown.markdown(md.get("0.1",END))
    root.clipboard_append(result)
    messagebox.showinfo("Information", "HTML CODE COPIED SUCCESSFULLY")

root = Tk()
root.iconbitmap("icon.ico")
root.geometry("700x510")
root.title("MD2HTML")
root.config(bg="black")

title = Label(root, text="MD-2-HTML",bg="#20B2AA",fg="white",font = ("Cascadia Code", 25))
title.pack(fill=X)

information = Label(root, text="Enter Your MD Code To Convert To HTML",bg = "black", fg="white")
information.pack(fill=X)

md = Text(root,borderwidth=0,bg = "#222222",fg = "white", insertbackground="#20B2AA",font=("Cascadia Code",9))
md.pack(pady=10)

go = Button(root, text = "Convert", border=0, command=go, bg = "lightgreen", activebackground="cyan")
go.pack(pady=2)

root.mainloop()

OS: Windows Python: 3.10

Thank you!

PS: In my code editor, that is VSCode, it's working fine...

lunix
  • 151
  • 1
  • 14
  • Since you're using the `pyinstaller` `--icon` command line argument, you probably don't need the `root.iconbitmap()` call in your script. – martineau Feb 06 '22 at 10:58

1 Answers1

2

You need to add a function to get the temp path for your exe file while it is running. Please check this link for details.

Bundling data files with PyInstaller (--onefile)

PS. I am using Pycharm, so I am not familier with VSCode. The pyinstaller command that I use is (you need to put your icon file and your py file in same folder):

pyinstaller --onefile --noconsole --clean --icon='icon.ico' --add-data 'icon.ico;.' MD2HTML.py

If you use Mac/Linux/Unix, please change 'icon.ico;.' to 'icon.ico:.'

You can check Adding Data Files for more information about *.spec file while using pyinstaller.

The code:

from tkinter import messagebox
import markdown
from tkinter import *
import sys
import os


def go():
    result = markdown.markdown(md.get("0.1", END))
    root.clipboard_append(result)
    messagebox.showinfo("Information", "HTML CODE COPIED SUCCESSFULLY")

# This function can get temp path for your resource file
# relative_path is your icon file name
def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


root = Tk()
iconPath = resource_path('icon.ico')
root.iconbitmap(iconPath)
root.geometry("700x510")
root.title("MD2HTML")
root.config(bg="black")

title = Label(root, text="MD-2-HTML", bg="#20B2AA", fg="white", font=("Cascadia Code", 25))
title.pack(fill=X)

information = Label(root, text="Enter Your MD Code To Convert To HTML", bg="black", fg="white")
information.pack(fill=X)

md = Text(root, borderwidth=0, bg="#222222", fg="white", insertbackground="#20B2AA", font=("Cascadia Code", 9))
md.pack(pady=10)

go = Button(root, text="Convert", border=0, command=go, bg="lightgreen", activebackground="cyan")
go.pack(pady=2)

root.mainloop()

The resource_path function code is copied from the first link which is showed above and writed by stack overflow user max.

Hope this can help you.

I have tried the code and there is no issues with it. Please make sure your pyinstaller version is up-to-date.

BTW: --icon=icon.ico command is for the exe file, and root.iconbitmap() in the code is for the icon that shows in the app window title bar(top left corner of the app window). They are not same.

Charles
  • 21
  • 3