Python 3 Couldn't open "<folderdirectoryhere>" : permission denied - image

Hoi! I was trying to access an image of mine using the following code:
import tkinter as tk
root = tk.Tk()
image = tk.PhotoImage(file="C:\\Users\*<mynamehere>*\\Documents\\Platformer")
label = tk.Label(image=image)
label.pack()
root.mainloop()
But I got the error stated in the title.
What's wrong, and how can I fix it? Any tips to load images in the future?

put an "r" before the file to open the raw file
image = tk.PhotoImage(file=r"C:\\Users\*<mynamehere>*\\Documents\\Platformer")

Related

How to use askopenfilename for .exe files?

I have a program with a GUI where you can search for Files.
I use the askopenfilename from Tkinter.
root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
After I built it into a .exe file with pyinstaller, I wanted to start it.
But if I start the .exe file, I see a window for a short while and then it closes instantly.
It helped not to use root.mainloop.
What can I do?
Thanks
Your problem is most likely an issue in the imports, you didn't include any so all I can do is assume.
This is what I usually would use:
(Tested working on python 3.8.1 x64, pyinstaller 3.6)
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir = "/",
title = "Select file",
filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
Made the .exe with pyinstaller.exe --onefile "path/to/script.py"

Writing out document in GUI executable using cx_freeze not working

I made an executable gui (with tkinter) file using cx_freeze. The executable has several buttons. When the user click the button, the calculation should work and then it will write out an xlsx file.
Everything went good when I make the executable, there was no error. But when I click the button, it seems like the calculation works (since it was loading), but then it does not write out the xlsx file.
I don't know what went wrong. Anyone can help me?
Here's the setup.py file:
import sys
from cx_Freeze import setup, Executable
import os
import tkinter
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [Executable("gui.py", base=base)]
packages = ["tkinter", 'xlsxwriter', 'matplotlib']
options = {
'build_exe': {
'includes': ["os", "tkinter", 'numpy.core._methods', 'numpy.lib.format', 'xlrd', 'scipy', 'pandas'],
'include_files': [r"C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll",
r"C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll"]
},
}
os.environ['TCL_LIBRARY'] = r'C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
setup(
name="Tool",
version="1.0",
description="Tool prototype for calculating",
options=options,
executables=executables
)
I've solved the problem.
What i did was to change the base in setup.py to : base = None
and I installed the xlsxwriter by following this thread: ImportError: No module named 'xlsxwriter': Error in bamboo
After this, I encountered another problem:
No module named: scipy.sparse.csgraph._validation
but I simply add this to the 'includes': ['scipy.sparse.csgraph._validation', ...]
now everything works perfectly.
I hope this helps anyone who has the same problem.

I cant make a dynamic image path in pdf

I've been trying to display an image in a fpdf file, it only works when I specify the exact location of the image. Can I make it dynamic?
Heres sample line of code:
$this->Image('fpdf/img/logo1.png',10,6,30);
and it works when I turn this way:
D:\Installed Apps\New
folder\XAMPP\htdocs\votingsystem\application\views\admin\senior\fpdf\img\logo1.png',10,6,30);
I want to transfer it to another laptop, I'm afraid it wont work.
I always get this error:
Message: fopen(fpdf/img/logo1.png): failed to open stream: No such
file or directory
I use code igniter as framework.
You can use multicell option in fpdf. I use this below code in php to it working.
`
define('ASSETS_URL','Your url or path to image file');
$yaxix = $pdf->GetY();
$xaxix = $pdf->GetX();
$pdf->MultiCell(50,10.5,$pdf->Image(ASSETS_URL.'/img/your-image-name.jpg',$xaxix,$yaxix+5,50)."\nAuthorised Signatory",1,'C',FALSE);
`
In this case you only have to change constants.

The image cannot be opened in python 3.6

I got question about the remote sensing image cannot be opened in Spyder(python 3.6), however, the weird thing is there are two "tif" files (Image.tif and Trend.tif) in the same directory and the Trend.tif which is the classification result of the Image.tif can be open with the code like this:
img= Image.open('C:/zijianwang/Final/2/Trend.tif')
but when I change the name to:
img1= Image.open('C:/zijianwang/Final/2/Image.tif')
I got the error:
OSError: cannot identify image file 'C:/zijianwang/Final/2/Image.tif'
Can somebody know what's going on here? Thank you so much!
My current PIL version is 4.2.1 and it is working for me. My code is as follows:
from PIL import Image
img = Image.open("{image-folder-path}/Trend.tif")

Tkinter: NameError: name 'tk' is not defined

I have been following tutorials of how to create a graphical user interface (GUI), in order to get used to it because I will use it in the future. The majority of tutorials use these commands at the first lines:
from tkinter import *
root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()
If I run this simple code I get the following error:
File
"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in
root=tk()
NameError: name 'tk' is not defined
After that I changed my code, so I used something like that:
import tkinter as tk
root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()
Now, the error is the following:
"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in
root=tk()
TypeError: 'module' object is not callable
Do you know what's going wrong? Could you help me please?
I'm using Spyder for Python 3.3, but I suppose it isn't a problem.
The tutorials you've seen is probably for Python 2. In Python 3 they've changed the naming conventions. So instead of root = tk() in P2, it's root = Tk() in P3 (Tk() is a class, hence the capital T).
In your second example your should write root = tk.Tk() after the import statement
I've just had a similar problem which I found out was because my Python console window in Spyder was connected to a different .py file that I was working on earlier so I closed it and opened a new python console in Spyder and the problem was gone.

Resources