Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Error] _tkinter.TclError: no display name and no $DISPLAY environment variable #4703

Closed
dr-nugget opened this issue Sep 9, 2021 · 6 comments
Labels
question Further information is requested

Comments

@dr-nugget
Copy link

Type of Issue (Enhancement, Error, Bug, Question)

Error


Operating System

Windows Subsystem for Linux (practically Linux in most cases)

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter (PySimpleGUI)


Versions

Version information can be obtained by calling sg.main_get_debug_data()
Or you can print each version shown in ()

Python version (sg.sys.version)

3.9.6

PySimpleGUI Version (sg.__version__)

4.47.0

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)

8.6.10


Your Experience In Months or Years (optional)

1 Years Python programming experience

2 Years Programming experience overall

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine) Not really

Anything else you think would be helpful?


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • [X ] Searched main docs for your problem www.PySimpleGUI.org
  • [X ] Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • [N/A ] If not tkinter - looked for Demo Programs for specific port
  • [N/A ] For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [X ] Run your program outside of your debugger (from a command line)
  • [X ] Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • [X ] Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

Hello,
I am trying out PySimpleGUI, so I made a "Hello World" program (below). When I ran it, I got the following error:
_tkinter.TclError: no display name and no $DISPLAY environment variable
So I try the demo Hello World on GitHub. And I got the same thing. It seems any PySimpleGUI program I run raises that error.

Code To Duplicate

import PySimpleGUI as sg
sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()

Screenshot, Sketch, or Drawing


Watcha Makin?

If you care to share something about your project, it would be awesome to hear what you're building.

@jason990420
Copy link
Collaborator

Know nothing about Linux system.

Just found something may help

The problem is that whatever you are doing when your program starts, the X-window system has not yet initialized and you get that error.

An application that creates an X-windows client can only usefully be run in an environment where it has access to an X-windows server to render the display in a graphical form.

$ printenv grep DISPLAY
localhost:0.0

if not specified, try to add following code before your run PySimpleGUI or tkinter code.

import os

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

Not for users for following code
Not sure if it will work if X-windows not yet initialized

setting = os.environ.__getitem__('DISPLAY')
window = tk.Tk(screenName = setting)

@dr-nugget
Copy link
Author

dr-nugget commented Sep 10, 2021

This didn't work. I got the following error:
_tkinter.TclError: couldn't connect to display ":0.0"

@jason990420
Copy link
Collaborator

Ok, it looks like you use Ubuntu LTS
Maybe you can refer
https://askubuntu.com/questions/993225/whats-the-easiest-way-to-run-gui-apps-on-windows-subsystem-for-linux-as-of-2018

WSL (Windows 10 Subsystem for Linux)) is not a full Linux installation with a Linux kernel, it allows you to run some native linux commands / binaries on a Windows (Microsoft) kernel and has limitations.
Install a 3rd party X server.

Tried to Install vcxsrv and start the server, and set the DISPLAY, then it OK for me.
https://sourceforge.net/projects/vcxsrv/

Maybe you can modify the environment

export DISPLAY=localhost:0.0

or

import os
import PySimpleGUI as sg

if os.environ.get('DISPLAY','') == '':
    os.environ.__setitem__('DISPLAY', ':0.0')

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read(close=True)

@jason990420 jason990420 added the question Further information is requested label Sep 10, 2021
@dr-nugget
Copy link
Author

X connection to :0.0 broken (explicit kill or server shutdown).
I have no experience with X-servers. It's probably easier to just develop on Windows.

@jason990420
Copy link
Collaborator

jason990420 commented Sep 11, 2021

First, it's not issue about PySimpleGUI or tkinter, but Windows Subsystem for Linux.
I also have no experience with X-servers.

Follow steps in your WIN10, and it work on my WIN10 and not sure if it work for everyone for maybe different configuration of WIN10.

  1. Install vcxsrv on your WIN10 from https://sourceforge.net/projects/vcxsrv/
    Maybe vcxsrv-64.1.20.9.0.installer.exe (41.783M)

  2. Start server (Maybe C:\Program Files\VcXsrv\xlaunch.exe) by click on Next, Keep everything default in it, don’t make changes. When you complete the display settings setup by clicking on next. VcXsrv icon will appear in the system tray, and, the Application will keep running in the background.
    image
    image

  3. Start Ubuntu in your WIN10,

jason990420@DESKTOP-ST6UJK4:~$ python3 hello.py
Traceback (most recent call last):
  File "hello.py", line 7, in <module>
    sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read(close=True)
  File "/home/jason990420/.local/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 8753, in read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
  File "/home/jason990420/.local/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 8819, in _read
    self._Show()
  File "/home/jason990420/.local/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 8578, in _Show
    StartupTK(self)
  File "/home/jason990420/.local/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 14746, in StartupTK
    Window.hidden_master_root = tk.Tk()
  File "/usr/lib/python3.8/tkinter/__init__.py", line 2270, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
jason990420@DESKTOP-ST6UJK4:~$ export DISPLAY=localhost:0.0
jason990420@DESKTOP-ST6UJK4:~$ python3 hello.py

image

# hello.py
import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read(close=True)

@tomplatner
Copy link

OK , I am a newbie but I Using PyCharm in Windows 11 and SSH to Raspberry Pi and I was getting the above error...this code suggested by Jason above did the trick for me.. specifically I added this code to the very beginning of my code:
import os
import PySimpleGUI as sg

if os.environ.get('DISPLAY','') == '':
os.environ.setitem('DISPLAY', ':0.0')

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read(close=True)

johnnymillergh added a commit to johnnymillergh/pysimplegui_boilerplate that referenced this issue Apr 27, 2024
_tkinter.TclError: no display name and no $DISPLAY environment variable

PySimpleGUI/PySimpleGUI#4703
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants