Get color scheme from GTK - python

I am developing a desktop app using Python 3 and GTK3.
I need a button to change its background color when hover, which can be done with this:
self.uploadbutton.modify_bg(Gtk.StateType.PRELIGHT, self.color)
Until now, I defined a RGB color that matches my own theme, but as I have to release this code, I would like to change that color to one of the established theme colors.
Searching the web, I found there's a way to do it with GTK2, but since version 3.8 of GTK, the gtk-color-scheme property has been deprecated.
Is there any other way to respect the user theme colors without using this property?

For now I have found two ways. Both of them are not the answer to your question, because as far as I understand in GTK3 every single element can have it's own styling (including color).
The first one is official and says not to mess with themes unless you have tested them and know how exactly your modifications would look. I think it's the best solution since it's not clear at compile-time whether your self.color will make it look ok or totally unreadable.
The second solution is to obtain full CSS and parse it yourself.
gset = Gtk.Settings.get_default ()
themename = gset.get_property ("gtk-theme-name")
prefdark = gset.get_property ("gtk-application-prefer-dark-theme") # it's a boolean
cprov = Gtk.CssProvider.get_named (themename)
print (cprov.to_string())
Parsing the theme is outside of this question.

Related

Vscode atom dark theme not highlighting tkinter

When I use from tkinter import * in vscode with atom one dark theme, I get no highlights from tkinter functions, though my code is still working.
In the picture, pack(), Tk(), Label and mainloop() is supposed to be highlighted (I know this, cause they are in the actual atom software).
Is there anyway to fix this?
Thanks!
There is nothing wrong with the code editor nor the theme. Its just how most of the theme works. Which part do you expect to be highlighted? All tkinter widgets(Label,Entry,etc.) are classes, and most themes do not have any highlighting for classes. I don't think much themes would have highlighting on class names and function names as its used very frequently.
"In the picture, pack(), Tk(), Label and mainloop() is supposed to be highlighted. Is there anyway to fix this?"
"Is supposed to be highlighted" is wrong words to use, as most themes does not highlight classes.
What usually has highlighting is, all the keywords and keyword arguments and string, etc. Since these are highlighted in your code, its working perfectly. I don't think there are any "special effects" for tkinter, as any theme for python is same as any theme for tkinter.
One way to get your desired effect, is to use a different theme, that actually will highlight classes (maybe monokai or some material themes?). I personally don't prefer alot of theme around, so something like this is perfect. Or the other way is to make or customize your themes, which can mess up the color if you are not sure what your doing.
I'm going to guess this is a problem on tkinters part and not the color theme but if it works on other themes and not one dark pro you can always try to change the exisiting colors using "yo generator code"
css-tricks does a good job showing how to install it: https://css-tricks.com/creating-a-vs-code-theme/
Altough they go through how to create your own theme. But you can always choose to import an existing theme.

Change color of __init__ and other predefined items in Pycharm's syntax highlighting

I'm colorblind and I can barely make out the __init__ when it is on the current line where the caret is, as in this example:
I just can't see it against that highlighted "caret line" background. Same problem for __repr__, __str__, and other such predefined items.
So, I'd like to change their color. How do I do that?
I looked through the color settings (pic below), but I can't figure out which one to change. None of them seem to do what I want.
Yes, I know I could change the color of 'caret row', but that's not a viable alternative, because __init__'s current color also gives me problems in other circumstances.
In PyCharm you can go to File > Settings...
In the settings panel you can go to Editor > Colors and Fonts and select Python.
Now you are given a code sample where __init__ is probably part of. If you click on the __init__ element, PyCharm will redirect you to an element called "Predefined item definition" which you can set to another color (the "Foreground" element in the right panel):
This is a screenshot of Pycharm Community on Ubuntu. I hope this helps.
Your settings panel seems to have a hierarchical view, based on the panel on the right it is probably located under Identifiers > Predefined symbol.
Changing this will change all predefined elements (like __len__, __class__, etc.) but since you say it is because of color blindness, you want probably to change these as well anyway.

matplotlib interactive alteration of line properties

I've been looking for a graphical / ipython console based means to turn lines on and off in a 2D graph generated with matplotlib, but I haven't found anything thus far.
Does anyone know a way to do something like this? What I have in mind specifically is incorporated in MATLAB, and can be seen here:
http://matlab.izmiran.ru/help/techdoc/creating_plots/plot_to5.html
All of the check boxes in the plot browser window will turn the lines on and off; their properties can also be altered graphically in another dialogue box. For now, I've been clicking on the properties button, and setting linetype to none, but this is cumbersome for a graph with many lines...
Thanks Vadim for your answer - you're right that the widgets provide an example with this functionality - to an extent. The example you provide doesn't give the graphical feedback I had in mind; instead, the widgets example closest to my request is actually check_buttons.py (see: http://matplotlib.org/examples/widgets/check_buttons.html)
Here, a side-box of labelled check buttons can be created, where upon clicking the checked buttons, it will turn the lines on and off - see the figure below. I suppose this could be built up into something along the lines of a plot browser like in matlab, but would require additional work to incorporate simple changes to the line style, etc.
I am still interested to know if someone has already done all of the work in making such functionality available; if not, I will post my best attempt when I get around to it.
plot_browser
I don't have sufficient rep points to add the image inline; my apologies.
Yes, there exists module named matplotlib.widgets. There are some example here. It allows you to do exactly what you asked for (source):

wxPython ,Change the background colour of a StyledTextCtrl

I tried (but nothing happens)
self.txt.SetBackgroundColour ((255,0,0))
As said in the title I'm trying to change the background colour StyledTextCtrl. Does anyone know a method that could be used? I've checked the API docs but I couldn't seem to find one, http://wxpython.org/docs/api/wx.stc.StyledTextCtrl-class.html
(by background colour, I mean the whole writing area, of course)
Does anyone know a way I could do this?
EDIT:
The background doesn't change in the following code
import wx
import wx.stc
app = wx.App(redirect=True)
top = wx.Frame(None, title="StyledTXTCtrl", size=(300,200))
txt=wx.stc.StyledTextCtrl(top)
txt.SetBackgroundColour((255,255,0))
txt.Refresh()
top.Show()
app.MainLoop()
My first reaction was to call txt.Refresh() because I had a similar experience using wx.TextCtrl where the background colour did not update and the Refresh() call forced a redraw. However, it seems that approach was incorrect.
After reviewing the StyledTextCtrl API, it seems like SetBackground() is not the function you want. My understanding is that because STCs can have multiple styles in the same box, the individual text styles take precedence over the STC's settings.
After some research I found the StyleSetBackground() function. This modifies the default style such that the background will be red, effectively setting the background to red. You need to call it like this:
txt.StyleSetBackground(wx.stc.STC_STYLE_DEFAULT, (255,0,0))
Just remember, if you use multiple styles you may need to make invoke this method for each one.
---EDIT---
I forgot to check my code code by entering some text. It turns out that if all you do is call SyleSetBackground() like I suggest above, when you enter text the background of the entered text is still white, not the expected red.
A bit more research and I've discoved this is easily fixed by calling the following code after setting the background colour:
txt.StyleClearAll()
My guess is that when you create the StyledTextCtrl, it sets the text style equal to wx.stc.STC_STYLE_DEFAULT. So far so good. However, after this point we change the value of the default style (by making the background red) so now the text style and the default style are different. Therefore, we need to call StyleClearAll() to reset ALL STYLES back to the default style (which now has a red background).
this works on my computer just fine
self.txt.SetBackgroundColour((255,0,0))
what is your OS?

In GTK on Windows, how do I change the background color of the whole app?

According to http://www.pygtk.org/docs/pygtk/gtk-constants.html, there are five state types: STATE_NORMAL, STATE_INSENSITIVE, etc. I want to set the background color of a Table, HBox, VBox, whatever, and I've tried setting every possible color of every kind of state:
style = self.get_style()
for a in (style.base, style.fg, style.bg,
style.light, style.dark, style.mid,
style.text, style.base, style.text_aa):
for st in (gtk.STATE_NORMAL, gtk.STATE_INSENSITIVE,
gtk.STATE_PRELIGHT, gtk.STATE_SELECTED,
gtk.STATE_ACTIVE):
a[st] = gtk.gdk.Color(0, 34251, 0)
Nothing has any effect. The only one that has any effect is when I manually created EventBoxes and specifically used the existing gtk.STATE_NORMAL color to blend with other colors. All the ones created by gtk without my intervention were not affected, though.
What's the proper way to go about doing this? I wouldn't mind having to make a gtkrc file or whatever. Is this because hbox, vbox, etc., don't have a color, but are transparent? Who provides the general color of the application, then?
Mostly google-fu (no windows here), posting as an "answer" mostly for a slightly better formatting. See if experimenting with the full-blown gtkrc like the one from here brings the fruits. The location, based on this and this appears to vary - so unless someone has the deterministic approach of finding it, filemon could be a sensible approach.
Being mostly a user for these kinds of apps, I would prefer the settings from my gtkrc over the hardcoded by what the programmer thought would be "the best", anytime.
It's because VBox and Hbox don't have an associated Window.
click here to see other widgets without windows. I would create event boxes and add the HBox or VBox inside the event box.
You need to call self.set_style(style) after changing the style. I used your code as a template and adding that line makes the background of everything green.

Categories