개발자/라즈베리파이4

_tkinter.TclError: no display name and no $DISPLAY environment variable 에러 해결

지구빵집 2021. 2. 9. 12:54
반응형

 

 

_tkinter.TclError: no display name and no $DISPLAY environment variable 에러 해결 

 

라즈베리 파이를 사용해 화면에 디지털 시계를 크게 표시하려고 자료를 찾았다. 실행하는데 에러가 계속 난다. 당연히 라즈베리 파이는 command 모드로 부팅을 하여 실습을 하고, 다른 컴퓨터에서 putty를 사용해 SSH에 접속하여 실습을 하였다. 전부 다 이런 환경으로 에러가 난 것이다. 으아~ 몇 시간을 또 잡아먹었다. 이렇게 삶은 낭비하는 것이다. ^^

 

아래와 같은 에러가 발생할 때 해결방법이다.

 

pi@raspberrypi:~ $ python3 digital_clock.py
Traceback (most recent call last):
  File "digital_clock.py", line 5, in <module>
    app_window = tkinter.Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

 

아래는 해결방법을 찾은 것을 그대로 옮기고 아래 해결방법을 따라서 하면 된다.

 

Re: help to start with tkinter
Quote
Sat Nov 05, 2016 8:40 pm

I did found the solution.

First you need to start up in GUI mode after reboot
You need to enter the GUI mode under the same login as the ssh connection (not the standard pi or you are logged in as pi at the ssh also)
then you need to execute at the ssh connection the command: export DISPLAY=:0.0
This forces display at hdmi

Than running the GUI programm from ssh the tkinter wil be displayed at the hdmi monitor

 

 

해결방법

 

1. 라즈베리 파이를 GUI 모드로 부팅을 하면 모든 문제가 사라진다. 즉 라즈베리파이를 GUI 모드로 부팅하도록 설정하여 x window로 부팅하여 커맨드 Terminal 을 실행하여 거기서 프로그램을 실행하면 제대로 프로그램이 실행된다. 여담이지만 만약 command 모드로 부팅하였다면 $startx 명령을 실행하여 x 윈도우를 실행하여 실습을 한다. ^^

 

2. 또한 다른 컴퓨터에서 SSH로 접속한 경우에는 우선 아래와 같은 명령으로 디스플레이 포트를 hdmi 화면으로 설정한다.

 

$export DISPLAY=:0.0 을 반드시 실행한다. 그 다음 시계 프로그램을 실행하면 다른 모니터 그러니까 라즈베리 파이에 연결된 HDMI 모니터에 아름다운 시계 화면이 나온다. 아래 명령을 실행한다.

 

pi@raspberrypi:~ $ export DISPLAY=:0.0
pi@raspberrypi:~ $ python3 digital_clock.py

 

이렇게 문제를 해결하면 된다. 도움 되었다면 뭐든 클릭을 해주시기 바랍니다. ^^

 

아래 터미널에서 명령을 내리면 위와 같이 HDMI 모니터에 출력

 

명령어 참고  export DISPLAY=:0.0

 

 

 

반응형