Pyinstaller EXE integrated with PNG background and ICO as icon - exe

Please I need to create a distributable EXE file as only ONE file from Python 3.7 code, that includes integrated a PNG image as a background and ICO image as an Icon using pyinstaller. So far, all the EXE files that I have created need the PNG and ICO files in order to be distributed and run, losing the main goal of the EXE as only ONE file distributed.
I did the setting of full path for files PNG and ICO in the code but the EXE only run in my pc and the files must be located in the specified path.
Even using AUTO-PY-TO-EXE the result is the same.
I need that EXE file has integrated the PNG and ICON files.
Please any suggestion and/or reference to any similar post solved.
For any test just use any PNG and ICO file including full path location.
from tkinter import *
root=Tk()
#set windows size
root.resizable(width=False, height=False)
root.geometry("925x722")
#set title
root.title("SOFT1)")
#frame 1
f1=Frame(root, width=345,height=475,bg="light
grey",highlightbackground="black",highlightthickness=4)
f1.place(x=20,y=235)
#set a image as BG
Logo=PhotoImage(file="PNG_File.png")
lab6=Label(root, image=Logo)
lab6.place(x=0, y=0)
#set a image as ICON
root.iconbitmap("ICO_File.ico")
mainloop()

I hope until now you found your solution, but I saw in another post that explained the same concept. I copied this function below and when i needed to create the image files in my app i used this function and inside the file name:
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
I tried afterward to run my .py file with auto-py-to-exe and that solved the problem for me.

From a brief look at what you're doing, you can't do that. Have you considered distributing your program as a .zip instead?
You can't do what you're trying to do because that load function is taking a file path - specifically a file path to the directory the script is run in, and searching for images in that directory. Without the images being there, it will fail on loading them. It's that simple.

Related

Images in Python exe file

I have created an app in which i am using png images as buttons. I included those images in the exe files as data when i was creating the exe file through pyinstaller, but those images are not shown in the exe file. I manually copied and pasted those images in the folder where the exe file is but still those images are not shown. what could go wrong any idea ? please

Use an imported font on PyGame

I want to add a font imported from google to Pygame.
I used that code but when I run script, the window immediately closes :
pygame.init()
pygame.font.init()
myfont = pygame.font.Font("resources/fonts/Mansalva.zip", 30)
The path to my .zip font is also good.
.zip is an archive. You've to unzip it and to load a single (e.g. .ttf) file from the archive. The filename which is passed to pygame.font.Font is assumed to be a font file. A zip-archive file can't be processed an causes an exception.

How can i open a file using a python script i coded?

I would like to open a file such as an image file using a python script, and by doing so, pass the file name and location into the script when i choose to open the image in my script.
clicks "open image in another program" in windows 10
import tkinter as tk
filename = fileopen.name()
filelocation = fileopen.path()
window = tk.Tk()
imagepic = tk.PhotoImage(file=filelocation)
picture = tk.Label(window, image=imagepic)
window.mainloop()
I'm pretty sure theres a module that allows for this but i just cannot find it.
I'm not entirely sure I understood your question correctly, but for dealing with the operating system you would use the module os:
import os
os.startfile(filelocation) # Open file in win10 default program
What I meant originally was how to go about registering a context menu option in windows explorer allowing the user to right click a file and select "Open with " - passing the file into the script as an argument.
In order to achieve this, you can follow this guide, supplying this command:
python -m <absolute/path/to/yourscript> %1
The %1 will be the file as an argument.

I need to add code to tkinter exe program?

I want to be able to show the GUI user the code used to do background calculations. However copying and pasting and using "\n" in a text box takes forever. Now I know you can save the code as a pdf. Is there a simple way to attach the pdf to the program and the code still be readable even when it is moved to a different computer.
Main issues:
- How to import and attach a pdf to a button...
- How to include pdf into program so it is readable on any pc...
Thanks in advance.
Packaging:
You can attach your code as pdf file with your program like this using pyinstaller
On Windows:
pyinstaller --add-data="relative/full_path_to_pdf;." my_script.py
On Linux:
pyinstaller --add-data="relative/full_path_to_pdf:." my_script.py
This will pack your pdf file and copy it to the same folder as the .exe package(in case of single file, it will extract it to the temp path along with main .exe which can be accessed with sys._MEIPASS) or You can change the extraction path instead of using '.' Read more here.
In Code:
You can add this type of button in your UI, to open the pdf file with the default viewer of Windows/Linux(same behavior as when you double click the file)
source_code_btn = Button(root, text="Source", command=lambda: subprocess.Popen('{} {}'.format(
"start" if os.name=="nt" else "xdg-open \"$#\" 2>/dev/null",
relative/full_path_to_pdf_file), shell=True))

How can I get a file or directory name from a file open dialog in PyQt4?

I am writing a PyQt4 application and one of the file types I wish to open is an Esri Grid format which, rather unusually, is a directory. I also wish to open other GIS filetypes that are just files (e.g. geotiffs). I can open these filetypes OK with the GDAL library by passing either a file or directory name and GDAL figures it out.
The problem I have is making the GUI. I want to open a file open dialog and get either a file name, or directory name. The problem is that the file dialog won't let me choose a directory - only files. I need the dialog to return a path to either. I've tried it on Mac and Linux.
I know PySide has a method called getExistingDirectory
http://pyside.github.io/docs/pyside/PySide/QtGui/QFileDialog.html
PyQt is basically the same, so it should have a similar method. http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html It is in the static methods section.
I think I've cracked it. This snippet tests the functionality I need:
dlg=QtGui.QFileDialog()
dlg.setFileMode(QtGui.QFileDialog.AnyFile);
e=dlg.exec_()
print dlg.selectedFiles()[0]
The solution was to set the file mode to 'AnyFile'. This allows the file dialog to return both directory and file names.

Resources