Advertisement
here2share

# py2any.py - A Versatile GUI ZZZ

Dec 3rd, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.60 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # py2any.py - A GUI to make executables from Python scripts ZZZ
  4.  
  5. '''
  6. So far, as a work in progress, this works with Tkinter and cx-Freeze. Had quite a difficult time trying to
  7. figure py2exe out while cx-Freeze took very little time. Will now attempt to properly make this script make
  8. it a much easier transition, then up next... hopefully get this to also build apk for AndroidOS devices, if
  9. feasible.
  10. '''
  11.  
  12. from Tkinter import *
  13. import tkFileDialog, tkMessageBox
  14. import subprocess, sys, os
  15. import time
  16.  
  17. cmdrefa='''#!/usr/bin/python
  18.  
  19. init=True
  20.  
  21. '''
  22.  
  23. # not sure "which" did the trick, however, the following is at your own risk...
  24. # set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5" set "TK_LIBRARY=C:\Python27\tcl\tk8.5" <<< most likely this
  25. # cmd >>> pip uninstall PIL >>> pip install PIL
  26. # *** https://pypi.python.org/packages/2.7/P/Pillow/Pillow-2.1.0.win32-py2.7.exe <<< make a backup beforehand
  27.  
  28. '''
  29. if CMD throws the error "ImportError: DLL load failed: %1 is not a valid Win32 application."...
  30. It means you have a 64 bit version of python, with a 32bit module, or the reverse
  31. '''
  32.  
  33. root = Tk()
  34. root.title("py2any")
  35. ws = root.winfo_screenwidth()
  36. hs = root.winfo_screenheight()
  37. cvx=600
  38. cvy=300
  39. x = (ws/2) - (cvx/2)
  40. y = (hs/2) - (cvy/2)
  41. #root.geometry('%dx%d+%d+%d' % (cvx, cvy, x, y))
  42.  
  43. shebang="#!/usr/bin/python" ### ZZZ if missing at top of target python script
  44.  
  45. incl_path=excl_path=icon_path='None'
  46.  
  47. def GetFile(event):
  48.     file = tkFileDialog.askopenfilename()
  49.     if file.split('.')[-1] in ('py','pyw'):
  50.         if 'setup.py' not in file: src.set(file)
  51.    
  52.     if 1: ### interference glitch? ZZZ
  53.         inFileBtn = Button(stepOne, text=browse)
  54.         inFileBtn.bind("<Button>", GetFile)
  55.         inFileBtn.grid(row=0, column=0, sticky='W', padx=5, pady=5)
  56.  
  57. def GetIcon(event):
  58.     icon = [tkFileDialog.askopenfilename()]
  59.  
  60. def make_checkbutton(frame, text, tick=NORMAL):
  61.     var = IntVar()
  62.     widget = Checkbutton(frame, text=text, variable=var)
  63.     widget.grid(sticky="NW")
  64.     if tick is 1: widget.select()
  65.     return var
  66.  
  67. def makePackage(event):
  68.     if src.get():
  69.         create_setup()
  70.     else: print('Host script not given')
  71.  
  72. def create_setup():
  73.     src_path=src.get()
  74.     if 1: ### ZZZ Not quite yet implemented.
  75.         excl_path='None'
  76.         if __excl_path.get():
  77.             excl_path="'C:\\Python27\\Lib'"
  78.     print('Folder In Explorer Should Open Automatically')
  79.     print('Double-click setup.bat to continue...\n')
  80.     os.startfile('C:\\Python27\\projects')
  81.     folder = "C:\\Python27\\projects\\" ### ZZZ
  82.     setupfile = folder +'setup.py'
  83.     source=src_path.split('\\')[-1]
  84.     sourcepath = folder + source
  85.     _ID_=source.split('.')[0]
  86.     print(_ID_)
  87.     cmdrefb="""if init:
  88.     import sys
  89.     from cx_Freeze import setup, Executable
  90.  
  91.     # Dependencies are automatically detected, but it might need fine tuning.
  92.     build_exe_options = {'packages': ['os'], 'excludes': ['tkinter']}
  93.  
  94.     # GUI applications require a different base on Windows
  95.     #   (the default is for a console application).
  96.    
  97.     setup(  name = '"""+_ID_+"""',
  98.             version = '0.1',
  99.             description = 'My GUI Application!',
  100.             options = {'build_exe': build_exe_options},
  101.             executables = [Executable('"""+_ID_+""".py')],
  102.             target_name = '"""+_ID_+"""',
  103.             target_dir='"""+_ID_+"""',
  104.             icon="""+icon_path+""",
  105.             data_files=None,
  106.             includes=None,
  107.             excludes=None,
  108.             bin_includes=None,
  109.             bin_excludes=None,
  110.             bin_path_includes="""+incl_path+""",
  111.             bin_path_excludes="""+excl_path+""")
  112.     """
  113.    
  114.     data = open(setupfile, 'w')
  115.     data.write(cmdrefa+cmdrefb)
  116.     data.close()
  117.     data = open(folder+'setup.bat', 'w')
  118.     data.write("python C:\\Python27\\projects\\setup.py bdist_msi & pause")
  119.     data.close()
  120.     # execfile(folder+'setup.bat') ### ? ZZZ
  121. #
  122.  
  123. mdetails="    Do Mouse Over For Details    "
  124. browse=" Browse... "
  125.  
  126. stepOne = LabelFrame(root, text=" 1. Script To Bundle: ")
  127. stepOne.grid(row=0, columnspan=7, sticky='WE', padx=5, pady=5)
  128.  
  129. helpLf = LabelFrame(root, text=" Quick Help ")
  130. helpLf.grid(row=0, column=9, columnspan=2, rowspan=8, sticky='NS', padx=5, pady=5)
  131. helpLbl = Label(helpLf, text=mdetails)
  132. helpLbl.grid(row=0)
  133.  
  134. stepTwo = LabelFrame(root, text=" 2. To Enter Details: ")
  135. stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipady=5)
  136.  
  137. stepThree = LabelFrame(root, text=" 3. To Configure: ")
  138. stepThree.grid(row=3, columnspan=7, sticky='WE', padx=5, pady=5)
  139.  
  140. stepFour = LabelFrame(root, text='Create Setup Command File (.bat)')
  141. stepFour.grid(row=4, columnspan=7, sticky='WE', padx=5, pady=5)
  142.  
  143. src = StringVar()
  144. inFileTxt = Entry(stepOne, textvariable=src)
  145. inFileTxt.grid(row=0, column=1, columnspan=7, sticky="WE", pady=2)
  146.  
  147. inFileBtn = Button(stepOne, text=browse)
  148. inFileBtn.bind("<Button>", GetFile)
  149. inFileBtn.grid(row=0, column=0, sticky='W', padx=5, pady=5)
  150.  
  151. LabelText=(' demo ',)*6
  152. order=0
  153. fldRowTxt="fldRowTxt^* = Entry(stepTwo)\n\
  154. fldRowTxt^*.grid(row=order, column=1, columnspan=5, padx=5, pady=2, sticky='WE')"
  155. for i in LabelText:
  156.     fldLbl = Label(stepTwo, text=i+str(order+1))
  157.     fldLbl.grid(row=order, column=0, padx=1, pady=1, sticky='W')
  158.     exec(fldRowTxt.replace('^*',str(order)))
  159.     order+=1
  160. __excl_path=fldRowTxt0
  161.  
  162. ChkBtnText=(' demo ',)*6
  163. order=0
  164. cfgspc=40
  165. chkstr="Chk^* = Checkbutton(stepThree, text=i+str(^*+1)+(' '*cfgspc))\n\
  166. Chk^*.grid(row=^*/2, column=^*%2, sticky='W', padx=1, pady=1)"
  167. for i in ChkBtnText:
  168.     exec(chkstr.replace('^*',str(order)))
  169.     order+=1
  170.  
  171. okaybutton = Button(stepFour, text="  Make Package  ")
  172. okaybutton.bind("<Button>", makePackage)
  173. okaybutton.grid(row=0, column=0, sticky='W', padx=5, pady=2)
  174. okaylabel = Label(stepFour, text="* Folder In Explorer Should Open Automatically  ")
  175. okaylabel.grid(row=0, column=1, sticky='W', padx=5)
  176.  
  177. mainloop( )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement