0

EDIT: Solved with "export DISPLAY=:0"

Original Problem:

I have a Display connected to my Raspberry Pi 3B+. It's working fine and I can see Raspberry's desktop.

I work on my PC on VSCode via SSH on my Raspberry. So when I start a python script, it runs on the Raspberry, but the terminal outputs are in VSCode on my PC. So far so good.

I programmed now a simple GUI with PyQt5 and want to display it on the Raspberry's display. But I always get the error:

qt.qpa.screen: QXcbConnection: Could not connect to display. Could not connect to any X display.

All the solutions out there are how to solve the tunneling, that the Raspberry can display the GUI on my PC monitor, but I want to have it just on the Raspberry's display which is working fine otherwise.

Any suggestions?

This is my Code, executed on the raspberry over ssh:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys


class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.initUI()

    def button_clicked(self):
        self.label.setText("you pressed the button")
        self.update()

    def initUI(self):
        self.setGeometry(0, 0, 100, 100)
        self.setWindowTitle("Tech With Tim")

        self.label = QtWidgets.QLabel(self)
        self.label.setText("my first label!")
        self.label.move(50,50)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("click me!")
        self.b1.clicked.connect(self.button_clicked)

    def update(self):
        self.label.adjustSize()


def window():
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

window()
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • When running from remote SSH you must set the `DISPLAY` environment, which usually is `:0.0` (it's normally automatically set on virtual terminals when started from X). Try with `export DISPLAY=:0.0` before running the script. – musicamante Nov 16 '20 at 23:15
  • This was it! Thanks a lot! – StraightCurlyCurves Nov 17 '20 at 23:25
  • Please, do not edit the question adding "solved" remarks. As the [tour](https://stackoverflow.com/tour) (which you should have followed) points out: "[StackOverflow i]s not a discussion forum.". You can add your own answer, or wait for other to give it, somebody found it out thanks to the comments like in this case. – musicamante Nov 17 '20 at 23:39
  • Note that, in this case, this is actually a duplicate question of another one: [Cannot connect to X server :0.0 with a Qt application](https://stackoverflow.com/questions/646930/cannot-connect-to-x-server-0-0-with-a-qt-application) (this also means that it will be probably closed). – musicamante Nov 17 '20 at 23:40

1 Answers1

0

set DISPLAY and XAUTHORITY variable environment before running your scrip. for example in systemctl service file:

[Unit]
Description=Start Clock

[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
ExecStart=/usr/bin/python3 /home/pi/clock.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity

[Install]
WantedBy=graphical.target
Hamid
  • 1,493
  • 2
  • 18
  • 32