Raspberry pi - startup tkinter script issue - python-3.x

I made a tkinter script (python3) which work perfectly when I launch it manually from the terminal.
I wanted to start it on launch so I modified the /etc/profile with :
sudo python3 /home/pi/script/main.py
The thing is that the script is launched but I only got a white screen as if my tkinter GUI wasn't working.
Does someone have an explanation ?

Okay I found the problem :
The tkinter script is looking for the ressources (images, etc.) in the folder in which it is executed.
By running python3 /home/pi/script/main.py tkinter will look for the ressources in the folder in which we execute from (as default it is the home directory (~)).
Tkinter didn't find an image so it stoped on the error (as any python script do)
I just had to change the directory with cd as so :
cd /home/pi/script
sudo python3 main.py

Related

Raspberry Pi tkinter app blank screen when run from script, works fine when run directly from terminal

I have created a weather forecast display application using Python and tkinter for Raspberry pi zero. The app pulls the weather from a weather api using requests and displays the forecast using images and labels. I run the app using python3 ./myappname.py and everything works fine.
I want this app to display automatically every time Raspberry Pi restarts. So I am writing a bash script that starts the application. The next step is to run the bash script on startup using cron or autostart. I have created a script with this code:
/bin/sleep 10 && /usr/bin/python3 /home/pi/myappname.py
I am running it using sh myscript
When I run this script, the app loads but there are no images or labels on the page, its a blank white screen. There are no error messages.
What could be causing the app to render fine when I directly run it but blank white screen when running with a script?
Most probably your script is loading images in relative path.
python ./myappname.py indicates that the current directory is the directory where the script is, so it works.
However the current directory may not be the python script directory when running the bash script at startup.
You need to change directory to the python script directory before running the python script inside the bash script:
#!/bin/bash
cd /home/hi
/bin/sleep 10
/usr/bin/python myappname.py

pyvirtualdisplay, cannot open host display

I am trying to run python script
from selenium import webdriver
from pyvirtualdisplay import Display
Display(visible=1, size=(800, 600)).start()
driver = webdriver.Chrome(options=option)
driver.get("https://google.com")
but I am getting error "Xephyr cannot open host display. Is DISPLAY set?
It's minimal ubuntu 18.04 installation with x11 installed
also chromium-browser works only when I type startx chromium-browser, otherwise it's drop Trace/breakpoint trap (core dumped)
#update
when I run sudo xinit and run my script manually it works but in window, not fullscreen, are there another way to run it?
#update
I was trying to set options in python script to run fullscreen like "--start-fullscreen" and "--start-maximized" but it cover half of screen like here: https://unix.stackexchange.com/questions/273989/how-can-i-make-chromium-start-full-screen-under-x
also I was trying to edit Preferences for chromium like in this thread in /home//.config/chromium/Default/Preferences, but its not working, I saw that every run of my python script chromium generate new Preferences in /tmp/.org.chromium.Chromium./Default/Preferences this browser setting are different than these in /home//.config/(...)/Preferences
ofc I have delete Display option from python script
Any help?

Trying to run script on boot, but "ModuleNotFoundError"

I've been struggling with this issue for a while, when I run python3 main.py my code runs, however when I try sudo python3 main.py it can't find the module that I've downloaded.
This hasn't been an issue until now, I want to run on boot, I have tried to edit the rc.local file, which needs superuser permission but of course it does not run without permission.
Also, when I install modules using the terminal, it doesn't seem to work at all, until I open thonny and download the modules that way, I can't see what i'm doing wrong.

Python3 GUI script does not work when double clicked

My GUI script that is a PyQt5 file (.pyw extension) does work when running on my IDE with a build configuration that tells the compiler to run the script with python3:
And it also works when i tell to the regular terminal on Linux to run same script with python3 like this:
When runned with the default python (python2.7) on a regular terminal it tells: ImportError: No module named PyQt5.QtWidgets.
My code does it have these lines on the start to tell that is a python3 script like: #!/usr/bin/python3 or #!/usr/bin/env python3 (I have python3 installed).
When double clicked on the Linux Mint File Explorer the cursor turns crosshair and nothing happends, with the terminal option, same happends and a empty terminal shows. Im talking these options
I guess Linux Mint still runs the scripts with python2.7 even when I added the bash lines to tell
Someone knows why the lines:
#!/usr/bin/python3
#!/usr/bin/env python3
doesnt work when just double click?
I want to run the script from the Linux File Explorer without the need of an IDE or using the terminal.
Try chmod +x file.py and run it in terminal by using ./file.py also try lunching the file from a different path, like python3 ~/path/to/file.py and see if the error persists

LXDE .desktop file permission issue

I’m trying to run a Python script via a desktop icon/shortcut/launcher on a Raspberry Pi using Raspbian and LXDE. I have to use a desktop launcher since the script has to be started via a tiny touchscreen only accepting left-clicks and without a keyboard.
I think that I have a permission issue since the script fails at a point where it should execute some system commands which require root.
The script works if I do the following:
Open a terminal
Enter sudo python3 program.py
I tried to replicate this behavior with a .desktop file using the following config:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=sudo python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
I suppose I missed something obvious, but since I don’t work with Linux usually I’m a bit lost here.
Edit: The problem was not a missing permission but an incorrectly assumed working directory and is now solved. Sorry for my confusion.
In detail: during development the script was started from its own folder while the desktop launcher used /home/pi as working directory. In general that's not a problem, but in the script a hardcoded path was used and a required file not found when using the launcher. The real problem was sloppy swallowed and the status code of a permission issue returned. So it was simply a bad code issue.
As docs for desktop entries says:
The Exec key must contain a command line. A command line consists of
an executable program optionally followed by one or more arguments.
The executable program can either be specified with its full path or
with the name of the executable only. If no full path is provided the
executable is looked up in the $PATH environment variable used by the
desktop environment. The name or path of the executable program may
not contain the equal sign ("="). Arguments are separated by a space.
It leads to conclusion, that maybe python3 is not within $PATH used by the desktop environment. Try to check full path of your python3 and sudo with:
whereis python3
whereis sudo
on my Archlinux it gives me /usr/bin/python3 and /usr/bin/sudo. Try to modify your Dekstop entry to something like:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=/usr/bin/sudo /usr/bin/python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
Let me know if this helps.

Resources