What is the best way to have transparency of specific widgets in a PyGTK application? I do not want to use themes because the transparency of each of the widgets will be changing through animation.
The only thing I can find is to use cairo to draw widgets with an Alpha, but I can't figure out how to do this. Is there perhaps a better way to do this as well?
Thanks!
Assuming that your program runs under composition manager, you could get per-widget transparency by manipulating widget's X window. Look at gtk.gdk.Window.set_opacity().
Note, it is not gtk.Window; you can get this object by getting its window property (buttonWidget.window), but only when widget is realized and only when widget does handle events -- gtk.Label does not have its own X window for instance.
If you need to work also when you don't have composition manager, drawing your widgets by yourself is the only option -- but you don't necessarily have to use cairo; drawing pixel by pixel on the bare X window will also work.
Related
I was working on Qt Designer and I wanted to edit the background color (if possible a gradient) of QScrollBar but I don't know how to do it properly.
When I add any background color the whole widget changes color uniformly and becomes solid.
I was trying to change the color of the area behind the bar and arrow buttons only.
Unfortunately, with many complex widgets you can't just change a property with stylesheet without all the necessary implementation.
QScrollBar is one of those widgets, and in order to achieve background customization through stylesheets, at least all the following have to be provided too:
overall background color
add/sub-page color
handle color
add/sub-line ("arrow buttons") colors
To make the widget more visually responsive, borders (with pseudo-states for pressed/enabled/hover states) should also be provided, and adding images for arrow buttons is usually suggested (small arrows can be obtained through css border tricks, though).
The starting point is the official Qt stylesheet example documentation, which has a section for customizing QScrollBar.
Note that using a QProxyStyle is not an option, as many styles draw scrollbars in different ways, and sometimes they even ignore some "official" style functions like drawControl, because they take care of the whole painting within the drawComplexControl, using private functions.
I'm having trouble getting the actual background color of widgets.
In my special case I'm having trouble with widgets within a QTabWidget.
This is on Windows7.
So classic widgets have some greyish background, whereas widgets within a tab are generally drawn with a white background.
I tried:
def bgcolor(widget):
color = widget.palette().color(widget.backgroundRole()) # version 1
color = widget.palette().color(QtGui.QPalette.Background) # version 2
rgba = color.red(), color.green(), color.blue(), color.alpha()
return rgba
which is pretty much what I could figure out myself from the Qt documentation and what google and SO give.
But, this just doesn't work.
I'm testing widgets inside and outside of a TabWidget and the function above returns identical colors for obviously differently colored widgets.
Namely it always returns the greyish color, even for the plain white colored widgets within the tab.
Am I missing something here?
EDIT:
My problem is when using matplotlib, matplotlib draws figures with a "none" facecolor with the wrong background color when embedded in a QTabWidget: grey, even though the parent widget is white.
To fix this I wanted to get the background color of the widget and set it as background color for the figure.
Though this may be a matplotlib issue, I guessed this would be the quickest workaround.
As I noticed I couldn't get the proper color, I became insistent :)
The palette is returning the correct colours.
The mistake you're probably making is that you're assuming "background" always means the same thing for all widgets. Let's take an unmodified QListWidget as an example. On a desktop with a traditional light-coloured scheme, this will probably appear as a white viewport inside a 3D sunken panel. But if you query the "background" for this widget, you will see something like this:
>>> widget = QtGui.QListWidget()
>>> widget.palette().color(QtGui.QPalette.Background).name()
'#edecec'
which is obviously not white. So Background is the wrong color role to query for this widget. Instead, it looks like Base might be more appropriate:
>>> widget.palette().color(QtGui.QPalette.Base).name()
'#ffffff'
It's worth noting that the documentation for color roles states that Background and Foreground are obsolete values, with Window and WindowText being recommended instead. Perhaps this is because the former terms are now considered to be misleading.
UPDATE:
On platforms which use pixmap-based styling, some of the reported palette colours will not match the visual appearance of a widget. This issue specifically affects Windows and OSX, and so may explain why you are not able to get the apparent background colour of tabs. This issue is documented in a Qt FAQ, which also gives some possible solutions (although the QProxyStyle option is supported in PyQt5 but not PyQt4).
I've read the questions about how to change a background, add a color, etc. in Kivy, and I'm familiar with how to do that for widgets.
My question is sort of a general strategy of what sort of object is intended to be used when all I want is a background that I'm going to put other widgets on---should that be a label or a widget, or a layout? (Suppose I'm only going to have a small padded background that I'll put a different colored gridlayout on, as in this question.)
I think the answer is really 'it depends'. As you say, you can add a background to anything, so the decision rests entirely on what kind of other behaviour you want the widget to have.
all I want is a background that I'm going to put other widgets on---should that be a label or a widget, or a layout?
If you want totally no extra behaviour, a Widget is fine. If you want simple behaviour like having the child automatically placed/size to fill the background widget, then of course a simple layout like BoxLayout will be ideal. Alternatively, you might want (for instance) the child to be made a little smaller than the background widget in order to get a little border, in which case an AnchorLayout would be just right. Or for arbitrary proportional behaviour, the FloatLayout is ideal.
Of course you can see there, the question really comes down to 'do you want layout behaviour', and if so you just pick the layout that does what you want. There's absolutely no limitation and rule, and it's in the design of kivy that you can combine behaviours like this to get precisely what you want.
(Suppose I'm only going to have a small padded background that I'll put a different colored gridlayout on, as in this question.)
In this case, an AnchorLayout seems ideal. This lets you set a padding for the child widget, but (by default) it's otherwise centered, so if you make your own AnchorLayout subclass with a background you'll get the border you seem to want.
Someone know if it's possible to change the color of a pixel in a canvas without using un object, so without using something like canvas.create_oval or canvas.create_rectangle ?
There is no way to color a pixel other than to create a 1x1 pixel object of some sort. And yes, at some point you will experience performance problems. The canvas simply wasn't designed to be used this way.
If you're really needing to create a large area in which you can manage individual pixels, you can create a canvas with a single image that is the same size as the canvas. You can then set the color of individual pixels on the image through the photo image interface.
Within tkinter itself, it's impossible.
Even if you manage to change a pixel on canvas window (which is possible with X11 and Windows APIs in a platform-dependent way), you'd have to ensure it's repainted properly.
You can, of course, place a frame of size 1x1 over the canvas, with a background color you want. This way, pixel is "changed" and no canvas object is created. If there's a real (though strange) problem behind a question, this trick could be a solution.
Ideally, the transparent border.
Here's an example of what i'd like to achieve:
Notice the transparent border.
Now i suppose I could use cairo to create a rectangle with transparency, and put a borderless non-transparent window inside, mimic'ing that effect - which I would if i knew the window would have a fixed dimension. However, if the inner window grows, it'll grow out of the transparent rectangle.
How should one approach such task?
Making window frames is really the job of the window manager (at least under X11, don't know how it works on windows).
But have a look at the GtkBin, GtkBox or GtkMisc widgets. Pack the dialog inside it as a single widget, and use padding to give it a size. Read up on GTK+ drawing model. You will probably need to set a flag and define your own expose-event handler to re-draw your frame.