3

I'm about to create a desktop based widget application using python via the gi.repository and the Gtk/Gdk v3.0. The goal is to have the window taking the whole screen being transparent, frameless and not accepting any focus. In addition it should be shown in every workspace and kept bellow any other window. After spending a long time I figured it out with the following code, yet with a drawback, the window has a vertical white line to the right side (kind of a border). So is there any solution to get rid of this line?

Os: Ubuntu 18.04 Python: 3.6

Screenshot

ui.py

import screeninfo
import gi

try:
  gi.require_version("Gtk", "3.0")
  gi.require_version("Gdk", "3.0")
  gi.require_foreign("cairo")
except ImportError:
  print("No Gtk v3.0 or pycairo integration")

from gi.repository import Gtk, Gdk, GLib

class Window (Gtk.Window):
  def __init__ (self, canvas):
    Gtk.Window.__init__(self)

    self.set_skip_pager_hint(True)
    self.set_skip_taskbar_hint(True)
    self.set_type_hint(Gdk.WindowTypeHint.DESKTOP)
    self.set_decorated(False)
    self.set_keep_below(True)
    self.set_accept_focus(False)
    self.set_can_focus(False)

    width = 500
    height = 500

    for monitor in screeninfo.get_monitors():
      if monitor.is_primary:
        width = monitor.width
        height = monitor.height

    # Todo: remove the annoying white border line on the right
    self.set_size_request(width, height)

    #Set transparency
    screen = self.get_screen()
    rgba = screen.get_rgba_visual()
    self.set_visual(rgba)
    self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 0))

    drawingarea = Gtk.DrawingArea()
    self.add(drawingarea)
    drawingarea.connect('draw', canvas.draw)

    self.connect('destroy', Gtk.main_quit)

    self.show_all()
    self.move(0, 0)

  def launch (self):
    GLib.timeout_add_seconds(1, self.refresh)
    Gtk.main()

    return True

  def refresh (self):
    self.queue_draw()

    return True

canvas.py

import cairo

FONT = 'UbuntuCondensent'
SLANT = cairo.FONT_SLANT_NORMAL
BOLD = cairo.FONT_WEIGHT_NORMAL

def draw (da, ctx):
  ctx.set_source_rgb(1, 1, 1)

  ctx.select_font_face(FONT, SLANT, BOLD)
  ctx.set_font_size(32)

  ctx.move_to(100, 100)
  ctx.text_path('Hi There!')
  ctx.fill()

main.py

import signal
import ui
import canvas

def main ():
  signal.signal(signal.SIGINT, signal.SIG_DFL)

  win = ui.Window(canvas)
  win.launch()

if __name__ == '__main__':
  main()
tzeikob
  • 31
  • 2
  • I can't reproduce this; as far as I can tell, absolutely nothing happens when I run the code. It just sits there, until I keyboard-interrupt it. I'm running an Ubuntu 20.04. – Sylvester Kruin Apr 14 '22 at 20:35
  • FYI, I'm running it on ubuntu 18.04, perhaps the code is functional only to that version. Possibly this has to do with gnome or gtk version. I think 20.04 is using gtk v4.0 where 18.04 works with gtk v3.0, not sure though. By keyboard-interrupt you mean ctrl+c right? – tzeikob Apr 15 '22 at 08:41
  • @SylvesterKruin I've just tested the code on ubuntu 20.04 via a clean vm installation and I can confirm that the issue doesn't get reproduced. It turns out that this behavior is only related to the v18.04. – tzeikob Apr 15 '22 at 13:07
  • Ah, okay. Sorry I can't help :-(! As far as I know, 20.04 uses Gtk 3, since Gtk 3 works on it, and Gtk 4 doesn't (tried using `gi.require_version("Gtk", "4")`, and it gave an error message). – Sylvester Kruin Apr 15 '22 at 14:56

0 Answers0