我发现了一个很老的帖子在桌面上显示 HTML 内容作为小部件。这是在这两个答案的帮助下更新的答案答案 1和答案-2.我尝试将其作为python脚本运行“python 文件.py“(如果我错了请纠正我,因为我没有背景知识,只是对这篇文章很着迷并试图获得可用的脚本)
因此,运行脚本后我遇到了一些错误,我通过 DuckDuckGoing 解决了这些错误,但仍然存在一些需要专家解决的错误。
当前脚本
我只是更新了前三行,将 WebKit 改为 --> WebKit2。
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import WebKit2, Gtk, Gdk, Gio, GLib
import signal, os
class MainWin(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 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))
#Add all the parts
self.view = WebKit2.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 = False
self.view.load_uri("file:///home/sai/Downloads/todo.html")
box = Gtk.Box()
self.add(box)
box.pack_start(self.view, True, True, 0)
self.set_decorated(False)
self.connect("destroy", lambda q: Gtk.main_quit())
#Show all the parts
self.show_all()
self.move(100,100)
def refresh_file(*args):
print args
mainwin.view.reload()
def file_changed(monitor, file, unknown, event):
# reload
GLib.timeout_add_seconds(2, refresh_file)
if __name__ == '__main__':
gio_file = Gio.File.new_for_path("/home/sai/Downloads/todo.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 work
Gtk.main()
现在的情况
我收到此错误并且无法解决。
#$ python file.py
Traceback (most recent call last):
File "file.py", line 51, in <module>
mainwin = MainWin()
File "file.py", line 23, in __init__
self.view.set_transparent(True)
AttributeError: 'WebView' object has no attribute 'set_transparent'
请帮助我获取工作脚本,以便将 HTML 页面显示为桌面小部件。我正在使用Ubuntu Mate 20.04。
答案1
仅供参考 - 原始脚本在 Ubuntu MATE 14.04 LTS(EoL)、16.04 LTS(EoL)和 18.04 LTS 上正常运行。
对于 Ubuntu MATE 20.04 LTS,您可以使用以下改编的代码:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import WebKit2, Gtk, Gdk, Gio, GLib
import signal, os
document="/usr/share/javascript/mathjax/test/sample.html"
class MainWin(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 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))
#Add all the parts
self.view = WebKit2.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 = False
# configure transparency as in https://github.com/LTSchmiddy/dungeon-commander/blob/8d5c58ca321a05519c8cb1c5afdebba4954cbfca/src/webview/platforms/gtk.py#L132
configure_transparency(self)
configure_transparency(self.view)
wvbg = self.view.get_background_color()
wvbg.alpha = 0.0
self.view.set_background_color(wvbg)
self.view.load_uri("file://"+document)
box = Gtk.Box()
self.add(box)
box.pack_start(self.view, True, True, 0)
self.set_decorated(False)
self.connect("destroy", lambda q: Gtk.main_quit())
#Show all the parts
self.show_all()
self.move(100,100)
def refresh_file(*args):
print args
mainwin.view.reload()
def file_changed(monitor, file, unknown, event):
# reload
GLib.timeout_add_seconds(2, refresh_file)
# configure transparency as in https://github.com/LTSchmiddy/dungeon-commander/blob/8d5c58ca321a05519c8cb1c5afdebba4954cbfca/src/webview/platforms/gtk.py#L549
def configure_transparency(c):
c.set_visual(c.get_screen().get_rgba_visual())
c.override_background_color(Gtk.StateFlags.ACTIVE, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.BACKDROP, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.DIR_LTR, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.DIR_RTL, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.FOCUSED, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.INCONSISTENT, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.INSENSITIVE, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.PRELIGHT, Gdk.RGBA(0, 0, 0, 0))
c.override_background_color(Gtk.StateFlags.SELECTED, Gdk.RGBA(0, 0, 0, 0))
transparentWindowStyleProvider = Gtk.CssProvider()
transparentWindowStyleProvider.load_from_data(b"""
GtkWindow {
background-color:rgba(0,0,0,0);
background-image:none;
}""")
c.get_style_context().add_provider(transparentWindowStyleProvider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
if __name__ == '__main__':
gio_file = Gio.File.new_for_path(document)
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 work
Gtk.main()
笔记:
答案2
生活比那容易多了!
一步步
google-chrome
在--app
模式下使用wmctrl
像这样:google-chrome --app="https://askubuntu.com/" && wmctrl -r :ACTIVE: -b add,below
就是这样...您的“小部件”就在您的桌面上...移动它、调整它的大小或关闭它。
为了使其透明,使用
xprop
像这样:xprop -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY "0xCCFFFFFF"
然后,触摸它。
要将其剥光,请使用
xprop
再次像这样:xprop -f _MOTIF_WM_HINTS 32c -set _MOTIF_WM_HINTS "0x2, 0x0, 0x0, 0x0, 0x0"
然后,触摸它 再次。
一次全部
您可以一步完成上述所有操作,如下所示:
google-chrome --disable-gpu --disable-software-rasterizer --app="https://askubuntu.com/" && \ wmctrl -r :ACTIVE: -e 0,0,0,550,800 -b add,below && \ xprop -id "$(xprop -root _NET_ACTIVE_WINDOW | awk '{print $5}')" \ -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY "0xCCFFFFFF" && \ xprop -id "$(xprop -root _NET_ACTIVE_WINDOW | awk '{print $5}')" \ -f _MOTIF_WM_HINTS 32c -set _MOTIF_WM_HINTS "0x2, 0x0, 0x0, 0x0, 0x0"
-e 0,0,0,550,800
设置窗口位置和大小...最后四个数字。请参见man wmctrl
-set _NET_WM_WINDOW_OPACITY "0xCCFFFFFF"
设置窗口透明度。更改CC
为以下之一这些改变透明度级别。