I have a code that uses tkinter
-a to receive input and generate a report (code result: one txt
and one xls
file). The code works, but when I convert it to exe
file (pyinstaller
library, tried auto-py-to-exe
also failed) file created but doesn’t work. What could be the problem?
import tkinter as tk
def get_data ():
date1 = dateFrom.get ()
date2 = dateTo.get ()
from pathlib import Path
print ("Start date")
print ("End date")
import os
import pandas as pd
mylist = []
for path in Path (). glob ('ot - *. log'):
if f'ot- {date1} .log '& lt; = path.name & lt; = f'ot- {date2} .log':
mylist.append (path.name)
import re
from datetime import datetime
with open ('final.txt', 'w', encoding = 'utf-8') as outfile:
for fname in mylist:
file_date = fname.replace ('ot-', '') .replace ('. log', '')
with open (fname, encoding = 'utf-8') as infile:
for line in infile:
outfile.write (file_date + '' + line)
sessions = {}
data = []
with open ('final.txt', 'r', encoding = 'utf-8') as f:
log = f.readlines ()
for line in log:
if 'Login successful' in line:
chunks = re.search (r "(. +?) Login successful from user (. +?) from. + session = (. +?). +", line)
login_time = datetime.strptime (chunks.group (1), '% Y-% m-% d% I:% M:% S% p')
sessions [chunks.group (3)] = {'login_time': login_time, 'username': chunks.group (2)}
elif 'Closing session' in line:
chunks = re.search (r '(. +?) Closing session. + session = (. +?). +', line)
logout_time = datetime.strptime (chunks.group (1), '% Y-% m-% d% I:% M:% S% p')
session_id = chunks.group (2)
if session_id not in sessions:
print ('Session {} closed, no login data'.format (session_id))
else:
login_time = sessions [session_id] ['login_time']
username = sessions [session_id] ['username']
session_time = logout_time - login_time
#print ('User {} entered {}, exited at {}. Session time- {}. Session ID- {}'. format (username, login_time, logout_time, session_time, session_id))
data.append ([username, login_time, logout_time, session_id, session_time])
del sessions [session_id]
df = pd.DataFrame (data, columns = ['user_name', 'login', 'logout', 'session id', 'session_time'])
df.to_excel ("test.xlsx")
win = tk.Tk ()
dateFrom = tk.Entry (win)
dateFrom.grid (row = 0, column = 1)
dateTo = tk.Entry (win)
dateTo.grid (row = 1, column = 1)
tk.Button (win, text = "Report", command = get_data) .grid (row = 2, column = 1)
Answer 1, authority 100%
The following steps allowed me to successfully build an EXE application using the fresh PyInstaller on Python 3.7 64-bit and Windows 10 64-bit, and this application eventually started and worked:
-
Add
tk.mainloop ()
to the end of the script (on the last line). Perhaps this in general will be enough for you to work. If not, follow the rest of the steps. -
Use only Python version
3.7
or less, if you don’t have it then install it. PyInstaller does not yet officially support Python3.8
or more. Although it is installed on these versions, it does not always work well or badly in the end. -
In python 3.7, upgrade PyInstaller to the latest version by the following command
C: \ bin \ Python37 \ python -m pip install --upgrade pyinstaller
, whereC: \ bin \ Python37 \ python
this is the full path to python 3.7, for example this is the path on my computer, it will be different on yours. -
In the folder where your script is located (let’s say it is called
script.py
) execute the commandC: \ bin \ Python37 \ Scripts \ pyinstaller --onefile --noupx script. py
whereC: \ bin \ Python37 \
is the full path with your python 3.7 installation, there will be a different folder on your computer. -
In the folder next to the script, the
Dist /
folder will appear in it. It will bescript.exe
, this is a startup file final, the entire program is one EXE file. Run it and try. I have such steps to produce a working and successfully executing EXE.