#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    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.

    Code:
    from Tkinter import *
    from decimal import *
    import re
    import os
    
    class Application(Frame):
      
        def __init__(self, parent):
            Frame.__init__(self, parent)   
             
            self.parent = parent
            self.grid()
            self.create_widgets()
    
        def create_widgets(self):
            """ Create widgets to get file name, size and type. """
            # create instruction label
            Label(self,
                  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
            Label(self,
                  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
            Label(self,
                  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
            Label(self,
                  text = "File extension:"
                  ).grid(row = 4, column = 1, sticky = W)
    
            # create variable for single, extension
            self.option = StringVar()
            self.option.set(".txt")
    
            # create radio buttons, extension
            options = [".txt",".pdf", ".xml",".zip"]
            row = 5
            for ending in options:
                Radiobutton(self,
                            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()
            self.variable.set("MB")
            
            # create a label MB|MiB radio buttons
            Label(self,
                  text = "MB or MiB:"
                  ).grid(row = 4, column = 2, sticky = W)
            
            # create radio buttons, var
            variables = ["MB", "MiB"]
            row = 5
            for var in variables:
                Radiobutton(self,
                            text = var,
                            variable = self.variable,
                            value = var
                            ).grid(row = row, column = 2, sticky = W)
                row += 1
    
            # create a submit button
            Button(self,
                   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)
    
        def create(self):
            """ 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."
    
            else:
                size=Decimal(size)
    
                if name=="":
                    name="untitled"
                    
                name+=opt
                directory = "output/test_files/"
                d = os.path.dirname(directory)
                if not os.path.exists(directory):
                    os.makedirs(directory)
    
                name=directory+name
                check = os.path.abspath(name)
                
                #the magic
                if var=="MiB":
                    with open(name,"wb") as output:
                        output.truncate(size * 1024 * 1024)
                else:
                    var="MB"
                    with open(name, "wb") as output:
                        output.truncate(size * 1000 * 1000)
                        
                message=name,size,var, check
    
            self.message_txt.delete(0.0, END)
            self.message_txt.insert(0.0, message)
            
    # main
    root = Tk()
    root.title("File creator (c) David Baak")
    app = Application(root)
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Add this line for Python2
    import FixTk
    or for Python3
    import Tkinter._fix
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Dietrich
    Add this line for Python2
    import FixTk
    or for Python3
    import Tkinter._fix
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Hello,

    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.

    Cheers,

    Razzer
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    thanks


    Originally Posted by razzerman
    Hello,

    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.

    Cheers,

    Razzer
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Small progress...


    Hey again,

    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...

    Cheers,

    Ray
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Good news!


    Hey again,

    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:

    name='/Users/python27/Desktop/RayMarch/Razzer.svg'

    os.path.abspath(name)

    Works just fine now.

    Cheers,

    Ray
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Aha


    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.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Partial Success


    Hey again,

    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).

    Cheers,

    Ray
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by razzerman
    Hey again,

    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).

    Cheers,

    Ray
    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...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Hey again,

    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).

    Ray

IMN logo majestic logo threadwatch logo seochat tools logo