Skip to content
Advertisement

pyinstaller makes an empty black window of my tkinter gui

For a very simple example, consider the following code

import tkinter as tk
from tkinter import messagebox


class App:
    def __init__(self, parent):

        # some widgets
        label = tk.Label(parent, text="Label")
        button = tk.Button(parent, text="button")
        label.pack()
        button.pack()

        # and a menu
        menu = tk.Menu(parent)
        parent.config(menu=menu)

        Menu1 = tk.Menu(menu)
        menu.add_cascade(label="Menu", menu=Menu1)
        for x in 'ABCD':
            Menu1.add_command(label="Menu " + x, 
                              command=lambda y=x: messagebox.showinfo(message=y))


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("250x100+100+50")
    root.title("My humble GUI")
    App(root)
    root.mainloop()


which produces a window like so:

enter image description here

After running pyinstaller --onefile --noconsole minimal.py (the name of the code file is minimal.py), I obtain, in the dist folder a bundle file (the app) which, when run, gives me the following:

enter image description here

The menu works just normally (that’s the reason for which I included it; it’s not really minimal, but it shows that something’s working…), but none of the widgets in the window are visible (not to mention the black color of the window, which I suppose is related to the problem).

It also happens that when I double click the app icon to open it, it tries to open it (the icon shows up in the dock bar for a second), and then it opens it for good after two seconds, more or less; this also doesn’t seem normal.

What should I do? Thanks

Advertisement

Answer

As I’ve started with Python through Jupyter (respectively Anaconda) but also had python installed through homebrew I ran into a similar issue as described by the OP. Meaning tkinter windows show fine through python interpreter but as soon as I created an executable with pyinstaller some of the windows just had wrong colors (all text was white for example).

I found this topic: Using pyinstaller with anaconda environment talking about it so went with:

conda create -n pyinstaller=3.6 -c defaults -c conda-forge
conda activate pyinstaller=3.6

but that didn’t really make pyinstaller work so I went to look further and uninstalled pyinstaller which I installed through pip and installed it through conda

pip uninstall pyinstaller
conda install -c conda-forge pyinstaller

I then tried to run stuff but I got some issue with external packages. So I checked pip list which didn’t show the packages anymore. Infact pip version shows something like this:

python3 -m pip --version                                                                                                                                       
pip 21.1.2 from /opt/anaconda3/envs/pyinstaller=3.6/lib/python3.9/site-packages/pip (python 3.9)

Not ideal :-D But after “pip installing” the packages that were missing, the windows started to show just fine after creating the executables with pyinstaller.

I guess if you run into this issue, you might just have a conflict of different python versions?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement