4

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

amrsa
  • 215
  • 2
  • 9
  • 1
    Why are you inheriting from `tk.Frame` if you aren't going to use it? – TheLizzard Apr 30 '21 at 12:19
  • @TheLizzard That's a good point. I just picked up part of the code from another program to make a small one, and I could have done it without that bit. Anyway, I've just tried without it, and as it should be expected, the problem persists... – amrsa Apr 30 '21 at 13:03
  • i tried it on windows 10 and it works. The onefile .exe looks exactly the same as if I ran it directly. Could it be a Mac thing? – Ramtin Nouri Apr 30 '21 at 13:53
  • @RamtinNouri Yes, that's probably the case. Hence the `macos` tag. Perhaps someone had the same problem and knows the solution. Thanks for your feedback, anyway! – amrsa Apr 30 '21 at 13:55
  • 1
    [this](https://github.com/pyinstaller/pyinstaller/issues/3765) seems to be the same issue. And maybe [this](https://github.com/pyinstaller/pyinstaller/issues/1350) is also related – Ramtin Nouri Apr 30 '21 at 14:02

4 Answers4

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?

lema
  • 80
  • 6
  • Thank you for your answer. Right now, I don't have the time to follow all those procedures and possibly get conflicting installations. Nevertheless, I appreciate your efforts as they seem reasonable and it's the first answer after all this time. The answer is probably correct, or so I trust and so I'm upvoting it. I'll delay a possible acceptance of the answer to when I'll find the time to follow through. Best wishes. – amrsa Jun 22 '21 at 10:37
  • further update to my previous comment: "conda activate pyinstaller=3.6" actually seems to switch to another python environment, something I didn't quite get before, then the standard "base" one or whatever your default config is. After rebooting I noticed that I was in the base py environment and nothing worked anymore. After activating my "pyinstaller=3.6" env (which btw. is a silly name for the env) I was able to run pyinstaller again, created executables and all was fine. – lema Jun 22 '21 at 15:11
1

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

Peter Choi
  • 11
  • 1
  • Thanks for your answer. I'll check on this soon, I hope. I've been busy with other things. I'll upvote your answer as a courtesy for your first answer. Then I'll see which one fits me best, among your answer and the previous one. But the previous one has precedence... All the best! – amrsa Jul 09 '21 at 18:20
1

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.

erosicky
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31608962) – C. Sederqvist Apr 30 '22 at 01:27
  • Solved my problem, thanks a lot! I just recently re-installed the Mac OS and PyCharm was using python 3.9 by default. Didn't make any code change but getting a blank window, quite confusing. – Chandler Nov 02 '22 at 20:50
0

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
Simon Henn
  • 199
  • 7