Reputation: 69
I have the following sample script coded in python 3.8.2:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class GridWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Grid Example")
grid = Gtk.Grid()
self.add(grid)
grid.set_row_spacing(2)
grid.set_column_spacing(4)
button1 = Gtk.Button(label="Button 1")
button2 = Gtk.Button(label="Button 2")
button3 = Gtk.Button(label="Button 3")
button4 = Gtk.Button(label="Button 4")
button5 = Gtk.Button(label="Button 5")
button6 = Gtk.Button(label="Button 6")
grid.add(button1)
grid.attach(button2, 1, 0, 2, 1)
grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
grid.attach(button5, 1, 2, 1, 1)
grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)
win = GridWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
That is only dummy example. I intend run this code through a cron entry on crontab file, but when I do that I get the following error:
Unable to init server: Could not connect: Connection refused
Traceback (most recent call last):
File "/home/dggt/Vscode/scratches/PythonExamples/gui_example/gi_config/sample_grid.py", line 31, in <module>
win = GridWindow()
File "/home/dggt/Vscode/scratches/PythonExamples/gui_example/gi_config/sample_grid.py", line 9, in __init__
Gtk.Window.__init__(self, title="Grid Example")
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 520, in __init__
raise RuntimeError(
RuntimeError: Gtk couldn't be initialized. Use Gtk.init_check() if you want to handle this case.
What makes this error more weird is the fact that when it is run through my IDE it perfectly works, in other words, the window with the buttons are display on screen. This is my specs:
This is my crontab entry:
This is my cron log header:
From dggt@dggt Mon Jun 29 11:21:01 2020
Return-Path: <dggt@dggt>
X-Original-To: dggt
Delivered-To: dggt@dggt
Received: by dggt (Postfix, from userid 1000)
id 4F71A87D05; Mon, 29 Jun 2020 11:21:01 -0300 (-03)
From: root@dggt (Cron Daemon)
To: dggt@dggt
Subject: Cron <dggt@dggt> /bin/python3 /home/dggt/Vscode/scratches/PythonExamples/gui_example/gi_config/sample_grid.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <PATH=/home/dggt/.local/bin:/home/dggt/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/dggt>
X-Cron-Env: <LOGNAME=dggt>
Message-Id: <20200629142101.4F71A87D05@dggt>
Date: Mon, 29 Jun 2020 11:21:01 -0300 (-03)
Has anybody any idea what could be going wrong? Have I missed some predefined configuration that had to be done before I could run this script?
Upvotes: 3
Views: 8914
Reputation: 36
The DISPLAY
environment variable is not set in crontab scripts. Only HOME
, LOGNAME
, and SHELL
are set.
A quick fix would be adding the variable in your cronscript. As discribed in Option 1 linked by @esbassi.
In your user session:
echo $DISPLAY
:0.0
In your crontab entry:
*/1 * * * * export DISPLAY=':0.0'; /bin/python3 /home/dggt/Vscode/scratches/PythonExamples/gui_example/gi_config/sample_grid.py
Upvotes: 2
Reputation: 8815
The crontab scripts are not launched in a full user session. The display server connection is not propagated to your script.
This is why you're getting this message:
Unable to init server: Could not connect: Connection refused
which then tells you that GTK could not initialise a display connection.
Upvotes: 0