0

I want to show a GUI Window (PySimpleGUI which is based on tkinter) as root user. I'm using pkexec for that. Using GNU/Linux Debian stable.

But I got the error

no display name and no $DISPLAY environment variable

I understand that a bit. But I don't know how to solve it. I tried to set DISPLAY = ":0.0" but this doesn't work either.

couldn't connect to display ":0.0"

This is my test call include setting DISPLAY.

pkexec python3 -c "import PySimpleGUI as sg;import os;os.environ['DISPLAY'] = ':0.0';sg.Window(title='title', layout=[[]], margins=(200, 100)).read()"

This is the full error output

No protocol specified
No protocol specified
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/dist-packages/PySimpleGUI/PySimpleGUI.py", line 10075, in read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
  File "/usr/local/lib/python3.9/dist-packages/PySimpleGUI/PySimpleGUI.py", line 10146, in _read
    self._Show()
  File "/usr/local/lib/python3.9/dist-packages/PySimpleGUI/PySimpleGUI.py", line 9886, in _Show
    StartupTK(self)
  File "/usr/local/lib/python3.9/dist-packages/PySimpleGUI/PySimpleGUI.py", line 16817, in StartupTK
    _get_hidden_master_root()
  File "/usr/local/lib/python3.9/dist-packages/PySimpleGUI/PySimpleGUI.py", line 16704, in _get_hidden_master_root
    Window.hidden_master_root = tk.Tk()
  File "/usr/lib/python3.9/tkinter/__init__.py", line 2270, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"
buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 1
    Try running `pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY python3 ''`. For more info look [here](https://unix.stackexchange.com/a/627007/456156) – TheLizzard Dec 18 '22 at 11:03
  • @thelizzard This works for my example. Thanks. But when using `os.execvp()`to start `pkexec` it doesn't work anymore. I'll investigate. – buhtz Dec 18 '22 at 12:21
  • 1
    I had the this issue a few weeks ago but I used `os.system` instead of `os.execvp`. Is there a specific reason for why you are using `os.execvp`? – TheLizzard Dec 18 '22 at 12:25
  • Yes because execvp does replace the existing process instead of just starting a new child process. – buhtz Dec 18 '22 at 12:26
  • @TheLizzard Please post your comment as answer and I will accept. [Here is a follow up question about the combined use with `os.execvp()`](https://stackoverflow.com/q/74841444/4865723). – buhtz Dec 18 '22 at 12:48

0 Answers0