Python Forum
Error when running script on startup in Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when running script on startup in Linux
#1
I'm trying to run a python program to display the screen on an SPI display. I'm on a Raspberry Pi 4 running Ubuntu. Before I run the script, I have to run the terminal command xhost + (something to do with setting the display). To make it run at boot, I put the script in rc.local with the xhost command before it:

export DISPLAY=:0 (set the DISPLAY variable)
xhost +
python3 /home/username/Desktop/SPI_Display.py >/home/username/Desktop/SPI.log 2>&1 & (run the script and send the output to a log file)

It works for about 140 frames, and then prints this:
Error:
XIO: fatal IO error 0 (Success) on X server ":0" after 899 requests (899 known processed) with 0 events remaining.
There is no traceback, it just prints that and stops. What's wrong?

Here's the script:
#!/usr/bin/python3

#imports
from PIL import Image, ImageDraw, ImageGrab
import os.path
import time
from Xlib import display
import adafruit_rgb_display.st7789 as st7789
import digitalio
import board
import mss

script_dir = os.path.dirname(os.path.abspath(__file__))#script path

cursor_image = Image.open(os.path.join(script_dir, "cursor.png"))#get image for cursor
cursor_image = cursor_image.resize(((int)(cursor_image.width / 5), (int)(cursor_image.height / 5)))#scale cursor image

#set pins
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)

BAUDRATE = 24000000

spi = board.SPI()

#display
disp = st7789.ST7789(
    spi,
    rotation=90,
    width=135,
    height=240,
    x_offset=53,
    y_offset=40,
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE
)

def mmsGrab(): #gets screenshot
    with mss.mss() as sct:
        for _, monitor in enumerate(sct.monitors[1:], 1):
            # Get raw pixels from the screen
            sct_img = sct.grab(monitor)

            # Create the Image
            img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
            return(img)

width = disp.height
height = disp.width

while True:
    start_time = time.time()
    image = mmsGrab()#grab screenshot
    
    #get cursor position
    cursor_data = display.Display().screen().root.query_pointer()._data
    cursor_pos = (cursor_data["root_x"], cursor_data["root_y"])

    image.paste(cursor_image, cursor_pos, cursor_image) #add cursor to screenshot

    image = image.resize((240, 135)) #resize to display
    disp.image(image)#display image
    #print FPS
    print("FPS: ", 1.0 / (time.time() - start_time))
    print("Display time: ", time.time() - start_time)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 573 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 461 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Is possible to run the python command to call python script on linux? cuten222 6 717 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Help Running Python Script in Mac OS emojistickers 0 333 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  error "cannot identify image file" part way through running hatflyer 0 662 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Trying to make a board with turtle, nothing happens when running script Quascia 3 654 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Using Autostart to run a GUI program at startup. Rpi Edward_ 1 617 Oct-28-2023, 03:19 PM
Last Post: SpongeB0B
  Syntax error while executing the Python code in Linux DivAsh 8 1,540 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Error when running kivy on python janeik 8 2,025 Jun-16-2023, 10:58 PM
Last Post: janeik
  Python script running under windows over nssm.exe JaroslavZ 0 704 May-12-2023, 09:22 AM
Last Post: JaroslavZ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020