I need to add code to tkinter exe program? - python-3.x

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

Related

Using Octave to "Edit" notepad file instead of "Open" in Windows

I use Windows 10 and an .exe program (in-house code written by a colleague) that imports data from .txt files. Since 99% of my use of .txt files are for this program, I've changed the default Windows program so that this .exe file is run automatically when opening a .txt file. If I need to access the .txt file directly, or use it for another purpose, I right-click and choose "edit."
I'm now writing a program of my own (using Octave 4.4.1), which also uses .txt files that sometimes need to be opened/edited, but if I use "open(filename)" in my Octave script, of course it just opens the .exe file. I can open the .txt file from there, but I'd like to skip this middle step, since the aforementioned .exe program is not intended to be used in this process, and there are other users of my code that don't have the .exe program installed.
Is there a way to duplicate the right-click/edit feature in Windows within Octave code? "edit(filename)" opens the file in the native Octave editor, which is technically viable, but not exactly a desirable scenario. I've also tried changing the default Octave editor to Notepad, and I've tried Notepad++ as well, but I have had absolutely no luck, even with significant effort, of making Octave use an external default editor of any kind (even when I remove the .exe program as the default for .txt files). Thanks in advance for any advice you can offer.
You can send command-line commands from Octave using the system() function.
For example, to open the file in notepad, you could do
[status, output] = system("notepad <path_to_text_file>.txt");
If notepad isn't in your system path, you will have to add it to or use the full path to the notepad executable
Or, if you want to use Notepad++, add it to your system path and then do
[status, output] = system("notepad++ <path_to_text_file>.txt");

Pyinstaller EXE integrated with PNG background and ICO as icon

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.

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.

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.

Opening Excel and PDf files with Tcl Tk

I am having problems opening an existing Excel file with Tcl Tk. I am able to open an existing MS Word file with no problems. The code that I am using is as follows, also my test application has "package require tcom" included:
proc OpenFile {} {
#Path to file
set app [::tcom::ref getobject "C:\\Users\\Me\\Desktop\\Test.doc"]
#Change path to application
set this [$app Application]
#Open application
$this Visible 1
}
This code is executed by a button. Basically, Test.doc gets opened after the button is pressed.
I tried changing the file to an existing Excel file, and when I press the button the file opens for a split second, and then closes. This also happens with MS Access files, as well.
Does anyone know how to open an existing Excel file with Tcl Tk, and make it stay open? Additionally, for PDFs and text files, I understand that I cannot use Tcom to open these files. Does anyone know how to open PDFs, text, and other non-MS files with Tcl Tk?
I really appreciate your help!
Thank you,
DFM
Assuming you're on Windows and you just want to open a file (.xls, .pdf, ...) with its usual application (ie. not modifying the file from your script) you can just use "start" like this:
set TestDoc "My Test.xls"
eval exec [auto_execok start \"\" [list $TestDoc]

Resources