How to write a transparent WebKit GTK application [closed] - webkit

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to write a GTK application, which just contains a GTK WebKit view in it. I hope the WebKit view is transparent, so the window below could be viewed, this feature is intented.
I found the answer in "Is it possible to render web content over a clear background using WebKit?" works on one of my PC, which is running Ubuntu with compiz, but does not work on another, which is running Debian with xfwm4. On xfwm4, it just shows a gray background when called webkit_web_view_set_transparent, when I did not call webkit_web_view_set_transparent, it just shows a white background. The testing HTML file is same on Ubuntu and Debian.
As far as I understand, to make the WebKit view transparent, I have to setup a compositing window manager.
To check whether my xfwm4 supporting compositing, I tried following code:
import gtk
import cairo
class MainWindow(gtk.Window):
__gsignals__ = {
'expose-event': 'override'
}
def __init__(self):
super(MainWindow, self).__init__()
self.set_app_paintable(True)
# no window border
self.set_decorated(False)
# see if we can do transparency
screen = self.get_screen()
alphamap = screen.get_rgba_colormap()
rgbmap = screen.get_rgb_colormap()
if alphamap is not None:
self.set_colormap(alphamap)
else:
self.set_colormap(rgbmap)
print 'sorry, no alpha channel available :('
self.set_size_request(300, 200)
def do_expose_event(self, event):
# we're going to draw on a temporary surface
# then copy to the window surface
# you can also draw directly on the window
width, height = self.get_size()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
# background is gray and transparent
ctx.set_source_rgba(.7, .7, .7, 0.75)
ctx.paint()
# red border
ctx.set_line_width(3.0)
ctx.rectangle(0, 0, width, height)
ctx.set_source_rgba(1.0, 0.0, 0.0, .75)
ctx.stroke()
# now copy to our window surface
dest_ctx = self.window.cairo_create()
# only update what needs to be drawn
dest_ctx.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
dest_ctx.clip()
# source operator means replace, don't draw on top of
dest_ctx.set_operator(cairo.OPERATOR_SOURCE)
dest_ctx.set_source_surface(surface, 0, 0)
dest_ctx.paint()
if __name__ == "__main__":
w = MainWindow()
w.show()
gtk.main()
This application shows a transparent bock with red border. So, I guess my Debian with Xfwm4 (version 4.8.3-1) should support compositing.
Anyone know why my GTK application could not be transparent?
===============
update:
I found the problem is in libwebkit, not in window manager. The versions of libwebkit in these two systems are different, and I tried my application another xfwm4 environment, the transparency works.

Related

In a GTK application using matplotlib, how can I set the color that appears while the figure is being redrawn after a window resize?

I can embed a matplotlib figure in a GTK application by following some of the examples, like this one in the matplotlib documentation. However, when I resize the application window, there is a flicker of white as the figure is being redrawn.
This isn't that much of a problem if the figure I am going to show also has a white background. But if I am using a dark figure style (i.e. to match the appearance of the rest of my application), there is always a white flicker until the figure is drawn in the correct style. This is somewhat jarring if the rest of the application is using a darker theme.
Is there a way I can control the background that is shown by a matplotlib backend while the figure is being drawn?
If I could set the color that appears while drawing, there would still be a flickering effect of the data and axes of the plot being redrawn, but at least the background would stay the same color before and after the figure is drawn.
To illustrate the unwanted effect, if I add a dark plot style to the example linked above, each time I resize the window I can see the white flicker.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from matplotlib.backends.backend_gtk3agg import (
FigureCanvasGTK3Agg as FigureCanvas)
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background') # use the dark style so flicker is more apparent
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_default_size(400, 300)
win.set_title("Embedding in GTK")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot()
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
ax.plot(t, s)
sw = Gtk.ScrolledWindow()
win.add(sw)
# A scrolled window border goes outside the scrollbars and viewport
sw.set_border_width(10)
canvas = FigureCanvas(fig) # a Gtk.DrawingArea
canvas.set_size_request(800, 600)
sw.add(canvas)
win.show_all()
Gtk.main()
(Resizing the window to very small sizes doesn't cause the flickering, presumably because once the figure is drawn at the smallest size, it does not get redrawn, just clipped.)
I'm using GTK 3, matplotlib 3.4.3, and Python 3.8.10 on Ubuntu.

matplotib: two overlaid transparent images look different when saved as svg or png

I'm trying to overlay two transparent images with matplotlib and save the result, but the result looks different depending on the file type. Specifically it's much more washed-out when saving to svg.
Here's an example. In this case, I could just add the two images before displaying them, but this is just a simple example. In reality what I'm trying to do is more complicated (images of different sizes with different colormaps), so they have to be plotted separately.
Example code:
f, ax = plt.subplots(figsize=(2,2))
ax.imshow(np.eye(3), alpha=.5)
ax.imshow(np.eye(3)[::-1], alpha=.5)
f.savefig('example.png')
f.savefig('example.svg')
The png file looks just like it does on the screen, but the svg file looks washed out. I would like to know how to save as svg, without the washed-out effect (i.e. it should look like it does on the screen).
As a bonus question, why does the png plot appear different depending on the order in which I plot the transparent images? The second image always looks stronger. Interestingly, in the svg, both are equally washed out.
Example saved as png:
Example saved as svg:
matplotlib version: 3.1.3
python version: 3.7.7
Thanks for any tips!
I'll post what I think is going on, but if someone can answer with more legit information I'll accept it.
I think that every time you call imshow with an alpha value, it blends the current image in the axis with the new image, using (new * alpha + current * (1-alpha)). The problem with this is that if you display 10 images each with alpha 0.5, then the first image is attenuated to nothing by the iterative blending, whereas the last image gets to be 50% of the final result. Nonetheless this is apparently the method used for rendering to the screen and saving to png.
In contrast, when saving to svg, it saves each image as a separate overlay with its own alpha. The svg container or renderer then uses some more intelligent method that considers all overlaid images at once. However, in my particular case, this leads to a more washed-out look because all the images are partially transparent.

Draw on GdkPixbuf without cairo in python gtk 3?

How do I draw on a GdkPixbuf object without using cairo? There are quite a few samples in gtk 2 that do this by obtaining a Pixmap and draw on it. However, I can seem to find any function that return a Pixmap in gtk 3. For example render_pixmap_and_mask() is no longer available for a Pixbuf object. How do I obtain a pixmap or any drawable from a GdkPixbuf in gtk 3 (similar to gdk_cairo_set_source_pixbuf)?

PyQt5 - setting the appearance of disabled QLabel pixmaps

I have a GUI with some QLabels which have a PNG image / pixmap each. When I set a process to perform I also added self.parent.setEnabled(False) and self.setEnabled(False) so that the user cannot interupt the process. However, the pixmap images turn grey and I would like to set them so that they stay as they are without a grey cast on them. How is this possible? Would a stylesheet be the solution?
My code to disable user interaction during the process is:
def playVideo(self):
global app
app.setOverrideCursor(QCursor(QPixmap("./imgs/cursor.png")))
self.parent.setEnabled(False)
self.setEnabled(False)
Many thanks for any help in this.

Matplotlib 3D pan and zoom not working

When i try to use the pan and zoom tool, the plot rotates on left click and zooms from the center on right click. zooming the plot by making a rectangular selection is not working.
Has somebody a clue how this problem can be solved?
Looking through the source code on github, it doesn't appear that the features you are looking for are supported. Hence you are unable to zoom and/or pan using mplot3d.Axes3D
def can_zoom(self) :
"""
Return *True* if this axes supports the zoom box button functionality.
3D axes objects do not use the zoom box button.
"""
return False
def can_pan(self) :
"""
Return *True* if this axes supports the pan/zoom button functionality.
3D axes objects do not use the pan/zoom button.
"""
return False
Source code: https://github.com/matplotlib/matplotlib/blob/master/lib/mpl_toolkits/mplot3d/axes3d.py
At the time of writing these are on lines 1017 through to 1031