'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



Solution 1:[1]

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?

Solution 2:[2]

I ran into this problem as well, and I was able to solve it by using the latest development version of pyinstaller. First I uninstalled pyinstaller with pip uninstall pyinstaller and then I used the following to get the latest version: pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Solution 3:[3]

This answer might be a bit late but for others with this issue: Just try different versions of pyinstaller. I don't think it's always possible to figure out what kinds of weird stuff is going on in the background. For me version 4.2 worked. Put this into your terminal:

pip uninstall pyinstaller    
pip install -Iv pyinstaller==4.2

Solution 4:[4]

I had this exact problem. I updated python from 3.8 to 3.10 and it solved it for me. No need to mess with pyinstaller.

If you're using macOS 12 Monterey, it's likely the culprit. See more from Python's website, along with a file to download 3.10 here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lema
Solution 2 Peter Choi
Solution 3 Simon Henn
Solution 4 erosicky