January 23rd, 2013, 02:13 PM
My file generator does not work after building with py2app
My script generates a file and places it in in a directory. When I run it in IDLE it works perfectly. When I build it in py2app, it does not create the directory nor does is create the file.
from Tkinter import *
from decimal import *
def __init__(self, parent):
self.parent = parent
""" Create widgets to get file name, size and type. """
# create instruction label
text = "Enter file size and name"
).grid(row = 0, column = 1, columnspan = 2, sticky = W)
# create a label and text entry for the size of a file
text = "File size: "
).grid(row = 1, column = 0, sticky = E)
self.calc_ent = Entry(self, width=12)
self.calc_ent.grid(row = 1, column = 1, sticky = W)
# create a label and text entry for the name of a file
text = "File name: "
).grid(row = 2, column = 0, sticky = E)
self.type_ent = Entry(self, width=12)
self.type_ent.grid(row = 2, column = 1, sticky = W)
# create a label File extension
text = "File extension:"
).grid(row = 4, column = 1, sticky = W)
# create variable for single, extension
self.option = StringVar()
# create radio buttons, extension
options = [".txt",".pdf", ".xml",".zip"]
row = 5
for ending in options:
text = ending,
variable = self.option,
value = ending
).grid(row = row, column = 1, sticky = W)
row += 1
# create variable for single, MB|MiB
self.variable = StringVar()
# create a label MB|MiB radio buttons
text = "MB or MiB:"
).grid(row = 4, column = 2, sticky = W)
# create radio buttons, var
variables = ["MB", "MiB"]
row = 5
for var in variables:
text = var,
variable = self.variable,
value = var
).grid(row = row, column = 2, sticky = W)
row += 1
# create a submit button
text = "Generate",
command = self.create
).grid(row = 9, column = 1, sticky = W)
self.message_txt = Text(self, width = 45, height = 2, wrap = WORD)
self.message_txt.grid(row = 10, column = 0, columnspan = 4)
""" Assign variables based on user input. """
# get values from the GUI
size = self.calc_ent.get()
name = self.type_ent.get()
var = self.variable.get()
opt = self.option.get()
#check if size is not a string with a regular expression
check = re.compile('\d+(\.\d+)?')
if check.match(size) == None:
message="Please enter a float (9.2) or an integer (3). Strings are not allowed."
directory = "output/test_files/"
d = os.path.dirname(directory)
if not os.path.exists(directory):
check = os.path.abspath(name)
with open(name,"wb") as output:
output.truncate(size * 1024 * 1024)
with open(name, "wb") as output:
output.truncate(size * 1000 * 1000)
root = Tk()
root.title("File creator (c) David Baak")
app = Application(root)
January 24th, 2013, 02:38 PM
Add this line for Python2
or for Python3
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
January 24th, 2013, 02:49 PM
The issue is not that the GUI does not work, the issue is that everything seemingly works, but my app does not generate the file/directory as it does in IDLE.
Originally Posted by Dietrich
January 30th, 2013, 03:24 AM
Thought I'd post and let you know that you're not alone. I've written a small Python program to create svg files from user input - works fine in IDLE and creates the svg file, but after compilation the app won't create the svg file. I'll keep looking and post here if I find an answer.
January 31st, 2013, 09:46 AM
Aha, so I am not alone...keep me posted if you find out what the solution is. It is py2qapp related but there seems no logical, straightforward answer.
Originally Posted by razzerman
February 12th, 2013, 02:11 AM
Just a quick update. Still no joy with py2app, but tried with pyinstaller (which is cross platform). Still no joy on the Mac side, but got the app to compile and run as it should on the Windows side - which was good news (and offers a glimmer of hope!).
Still working on the mac side...
February 21st, 2013, 11:44 AM
Success! Finally got the Mac version to work. I was hoping the app would know where to put the created file. It didn't. Had to add the following:
Works just fine now.
February 24th, 2013, 02:05 PM
Well done finding out
The only issue is that it works on your own mac but if you install the app elsewhere it will not work because your path is hardcoded.
But OK, at least it does something, more than it did before.
March 5th, 2013, 05:24 AM
I didn't point out that I only got it working with PyInstaller, not Py2App (which still refuses to work).
I looked through your code and noticed you already had the paths specified (more elegantly than my brute force method).
March 5th, 2013, 05:35 AM
Then it seems it is a stupid py2app bug no one knows how to solve. I asked on stack overflow, but one knew the answer either...
Originally Posted by razzerman
March 12th, 2013, 06:54 AM
More good news - it does work now with py2app. Updated to py2app 0.7.3, and it worked fine (as long as path exists leastways).