wxPython ,Change the background colour of a StyledTextCtrl - python

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?

Related

Using ttk theme azure-dark and Changing background color of ttk combobox widget

I am using the ttk azure theme dark Azure-ttk-theme. It sets the background color to nice modern looking UI. However, as seen in the images the background color of the tk window, Text widget and combobox widget is set as same. This looks bad when we are using all these together with some text in area(as seen on the right side of screenshot below).
I tried to modify the background of my combobox as below(pardon the full imports- just for replication):
from tkinter import *
from tkinter.ttk import *
root = Tk()
style = ttk.Style(root)
root.tk.call("source", SYSTEM_DIR / "azure.tcl")
root.tk.call("set_theme", "dark")
style.configure("TCombobox", fieldbackground= "orange", background= "white")
g_combo = Combobox(root, style="TCombobox")
But, the above code does not have any effect on my interface. If someone has any idea on how I can handle this. I might have to modify the tcl file for this theme. Since, I have limited knowledge on tcl I am looking for answers/suggestions here.
Ahh, well, I am answering this question because I also tried using this theme yesterday. My idea was too something similar to yours. I wanted to change the default colors and stuff to get a better UI. Before getting into the solution, I have to tell you that this theme overrides the default values and designs to a custom-created one as set by the author. So here's what you can do to achieve what you want.
As far as I know, the only way to do this is to edit the TCL file, which manages the colors of the widgets. It would help if you had some idea about TCL language before you get into it so deep. So I will tell you step by step:
Go to dark.tcl file, and then find the widget's code where you want to change the background color. Say you want to change the combo box's background color. So if you are on windows, you can search "Combobox" by pressing ctrl+f after opening that file (I suggest using Notepad to edit it, it's more accessible).
Ok, so here are the codes where you want to change. As you can see in the third part of the code (ttk::style element create Combobox.field), it is used to create the combo box widget. You can keep trying changing different statements and customize the whole Combobox and its effects (If you know how to). But now lets focus on background color
So in there, you see that the default background is set to an image known as box-basic.
So now you have to go to Azure-ttk-theme/theme/dark folder and find out where that box-basic file is. Once you find it, you have to edit the colors of that image with respecting the same size and then replace the old one and make sure you set the same name. I suggest you to use figma to replicate another image. Its easy. Change it to any color you wish.
And you are done!
With some other theme fieldbackground would work, but not with Azure (or with any of my themes).
Tk handles PNG images containing transparency very badly, especially on Microsoft Windows.
So to reduce lagging, I removed the transparent areas of the image elements, and therefore where you'd expect fieldbackground to be, there's actually the images' background, which looks like as if it were fieldbg.
So the solution is to edit the images, as TechieGeeke suggested.

How do I create something as shown below (volume change indicator) in linux?

I would like to show an image with transparent background to indicate something when a key combination is pressed.
Let's say I pressed ctrl+f3, I trigger a python script. Is there anyway I can make that happen?
What python library can I use to show an image without window border and background?
I have figured out how to trigger the file on key press. How to I deal with the (imshow) thing?
Thank you.
show an image without window border and background
This sound like task for some GUI library. There are many available but you would need test them in order to find which one can do it. First feature is generally known as frameless or borderless window. tkinter which ships with python has ability to work this way, see for example tutorialspoint.com tutorial, though I do not know how it will work with alpha channel of your image.

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.

Get color scheme from GTK

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.

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