Quantcast
Channel: as days pass by
Viewing all articles
Browse latest Browse all 158

Writing a simple desktop widget for Ubuntu

$
0
0

I needed a way to display the contents of an HTML file on my desktop, in such a way that it looks like it’s part of the wallpaper. Fortunately, most of the answer was in How can I make my own custom desktop widgets? on Ask Ubuntu, along with Create a Gtk Window insensitive to Show Desktop and Won’t show in Launcher. Combining that with the excellentPython GIAPI Reference which contains everything and which I can never find when I go looking for it, I came up with a simple little Python app. I have it monitoring the HTML file which it displays for changes; when that file changes, I refresh the widget.

fromgi.repositoryimportWebKit,Gtk,Gdk,Gio,GLibimportsignal,osclassMainWin(Gtk.Window):def__init__(self):Gtk.Window.__init__(self,skip_pager_hint=True,skip_taskbar_hint=True)self.set_wmclass("sildesktopwidget","sildesktopwidget")self.set_type_hint(Gdk.WindowTypeHint.DOCK)self.set_size_request(600,400)self.set_keep_below(True)#Set transparencyscreen=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))#Add all the partsself.view=WebKit.WebView()self.view.set_transparent(True)self.view.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0,0,0,0))self.view.props.settings.props.enable_default_context_menu=Falseself.view.load_uri("file:///home/aquarius/Work/jobs.html")box=Gtk.Box()self.add(box)box.pack_start(self.view,True,True,0)self.set_decorated(False)self.connect("destroy",lambdaq:Gtk.main_quit())#Show all the partsself.show_all()self.move(100,100)defrefresh_file(*args):printargsmainwin.view.reload()deffile_changed(monitor,file,unknown,event):# reloadGLib.timeout_add_seconds(2,refresh_file)if__name__=='__main__':gio_file=Gio.File.new_for_path("/home/aquarius/Work/jobs.html")monitor=gio_file.monitor_file(Gio.FileMonitorFlags.NONE,None)monitor.connect("changed",file_changed)mainwin=MainWin()signal.signal(signal.SIGINT,signal.SIG_DFL)# make ^c workGtk.main()

Lots of little tricks in there: the widget acts as a widget (that is: it stays glued to the desktop, and doesn’t vanish when you Show Desktop) because of the Gdk.WindowTypeHint.DOCK, skip_pager_hint=True, skip_taskbar_hint=True, and set_keep_below(True) parts; it’s transparent because the HTML file sets its background colour to rgba(0,0,0,0) with CSS and then we use override_background_color to make that actually be transparent; the window has no decorations because of set_decorated(False). Then I just add it to Startup Applications and we’re done.


Viewing all articles
Browse latest Browse all 158

Trending Articles