0
votes

I am trying to build a one-file EXE file with Pyinstaller that opens a tkinter GUI. For some reason though, the EXE does not open any tkinter windows when ran. I wrote a quick code to test if Pyinstaller was having issues with the tkinter module. It looks like this:

import tkinter

root= tkinter.Tk()

canvas1 = tkinter.Canvas(root, width = 300, height = 300)
canvas1.pack()

def hello ():  
    label1 = tkinter.Label(root, text= 'Hello World!', fg='green', font=('helvetica', 12, 'bold'))
    canvas1.create_window(150, 200, window=label1)
    
button1 = tkinter.Button(text='Click Me',command=hello, bg='brown',fg='white')
canvas1.create_window(150, 150, window=button1)

root.mainloop()

When tkinter is the only module being imported, the EXE file built by Pyinstaller runs without issues. But when I add all of the other imports that my GUI program uses, the compiled one-file EXE no longer opens the tkinter window:

import tkinter
import tkinter.filedialog
import os
import PIL
import numpy as np
import math
import win32api
import ctypes
import peakutils
import matplotlib
import skimage.measure
import scipy
import astropy.modeling
import cv2
import sklearn.neighbors.NearestNeighbors
import imutils
import sys

...

I read the documentation of the imported modules, and there are some duplicate dependencies (astropy.modeling imports scipy, for example). Is this kind of double-dependency why Pyinstaller isn't able to build a working EXE, or is there another reason? I realize that there are a lot of imported modules, could this also be why the EXE won't work? How can I fix this? I should also add that Pyinstaller didn't return any errors when building either of the EXEs.

1

1 Answers

0
votes

The main problem here is "where is your .exe file located?".

When you run some command with pyinstaller, it will create several folders with your needed modules. For example:

You have an Application named your_app.py that needs these modules:

  • Pandas
  • Tkinter
  • Numpy
  • Google Cloud Platform

Then you run your pyinstaller command, for example:

pyinstaller -F --onedir --windowed your_app.py

The above command will generate a dist folder with all your dependencies. Inside that folder, you will find your .exe file.

Here is the problem:

If your application is using information from relatives path (for example read a csv file from some path in your pc) and you don't put that folder inside the dist folder, you will get a traceback.

From here, the BEST way to figure out your problem, is by generating a .exe file with the console, in order to debug your problem.

You can achieve this by deleting the keyword --windowed from your pyinstaller command:

pyinstaller -F --onedir your_app.py