tkinter.TclError: no display name and no $DISPLAY environment variable python

Question:

I want to execute my python file in a remote system using ssh. I exported the file to the remote system.

Here’s the sample file:

import os
import time
import pymsgbox

pymsgbox.alert('Hi Afreeth ', 'Welcome')
if 'DISPLAY' not in os.environ: 
    pass

I want to execute it using ssh from my system and it should display in the remote system.
But it fails

Error i got:

Traceback (most recent call last):
  File "cd1.py", line 5, in <module>
  File "pymsgbox/__init__.py", line 100, in alert
  File "pymsgbox/__init__.py", line 156, in _buttonbox
  File "tkinter/__init__.py", line 1871, in __init__
_tkinter.TclError: no display name and no $DISPLAY environment variable
[12113] Failed to execute script myprogram

How to fix it. I found some answers on stack but it doesn’t solve me. If i go and execute it in the remote system, it works. But when i execute from my system,it fails. How to fix it.

Asked By: Smack Alpha

||

Answers:

Found the Answer:
I just need to run export DISPLAY=:0 in their ssh session and programs run will run on the remote display. A quick example:

paulsteven@smackcoders:~$ ssh afreeth@his_ipaddress
afreeth@smackcoders:~$ export DISPLAY=:0
afreeth@smackcoders:~$ firefox
Firefox is now running on afreeth's display.
Answered By: Smack Alpha

you can use it with python instead of shell

import os

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

Note based the display is :0 to make sure on GUI run on GUI terminal echo $DISPLAY
source

Answered By: Zaman