Thread overview |
---|
June 15, 2016 GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Hello, why does this code not work? RGBA rgb = new RGBA(1,0.5,0.5,1.0); Button btn_1 = new Button("Start"); btn_1.overrideBackgroundColor(StateFlags.NORMAL, rgb); The color of btn_1 just doesn't change. |
June 15, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Wednesday, 15 June 2016 at 09:03:45 UTC, TheDGuy wrote: > Hello, > why does this code not work? > > RGBA rgb = new RGBA(1,0.5,0.5,1.0); > Button btn_1 = new Button("Start"); > btn_1.overrideBackgroundColor(StateFlags.NORMAL, rgb); > > The color of btn_1 just doesn't change. https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-override-background-color |
June 15, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerald | On Wednesday, 15 June 2016 at 20:49:02 UTC, Gerald wrote: > On Wednesday, 15 June 2016 at 09:03:45 UTC, TheDGuy wrote: >> Hello, >> why does this code not work? >> >> RGBA rgb = new RGBA(1,0.5,0.5,1.0); >> Button btn_1 = new Button("Start"); >> btn_1.overrideBackgroundColor(StateFlags.NORMAL, rgb); >> >> The color of btn_1 just doesn't change. > > https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-override-background-color Thanks for your reply, i now tried to use CSS instead: import std.stdio; import std.file; import gtk.Main; import gtk.MainWindow; import gtk.CssProvider; import gtk.Button; import gdk.Display; import gdk.Screen; import gtk.StyleContext; class Window : MainWindow{ this(int width, int height, string title){ super(title); setDefaultSize(width, height); Button btn = new Button("Test"); btn.setName("CssName"); string cssPath = "test.css"; CssProvider provider = new CssProvider(); provider.loadFromPath(cssPath); Display display = Display.getDefault(); Screen screen = display.getDefaultScreen(); StyleContext.addProviderForScreen(screen, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); add(btn); showAll(); } } void main(string[] args){ writeln(getcwd()); Main.init(args); auto win = new Window(250,250,"Tutorial"); Main.run(); } This is my CSS: GtkWindow{ background-color:blue; } #CssName{ -GtkWidget-focus-line-width:0; background-color:green; color:green; } The text color is green but the button background color is still default-gray! I am also wondering how it is possible to change the button color at runtime? In my opinion i don't think that CSS-based style has alot of advantages over the commonly used object functions. |
June 16, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | > string cssPath = "test.css"; > > CssProvider provider = new CssProvider(); > provider.loadFromPath(cssPath); unfortunately i don't know anything about yr specific problem. but i just wanted to mention (in case you are not aware of it) that the CSS can be embedded into the D source. this is what i did to fix GTKs terrible design mistake for the background of Notebook: ``` enum myCSS = q{ GtkNotebook { background-color: #e9e9e9; } GtkNotebook tab { background-color: #d6d6d6; } }; ... int main(string[] args){ ... import gtk.CssProvider; auto styleProvider = new CssProvider; styleProvider.loadFromData(myCSS); import gdk.Screen; import gtk.StyleContext; StyleContext.addProviderForScreen( Screen.getDefault(), styleProvider, 800); ``` |
June 15, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Wednesday, 15 June 2016 at 21:39:37 UTC, TheDGuy wrote: > On Wednesday, 15 June 2016 at 20:49:02 UTC, Gerald wrote: >> On Wednesday, 15 June 2016 at 09:03:45 UTC, TheDGuy wrote: >>> Hello, >>> why does this code not work? >>> >>> RGBA rgb = new RGBA(1,0.5,0.5,1.0); >>> Button btn_1 = new Button("Start"); >>> btn_1.overrideBackgroundColor(StateFlags.NORMAL, rgb); >>> >>> The color of btn_1 just doesn't change. >> >> https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-override-background-color > snip... > The text color is green but the button background color is still default-gray! I don't see an obvious issue with your code, I usually use CSS classes personally and I know that works fine because I use this technique all over terminix. I would suggest using the GTK Inspector to debug the CSS issue, it's an awesome tool for figuring out GTK CSS issues as it let's you change CSS on the fly, see what CSS is being applied to an object, etc. You can see how to use it at the link below: https://wiki.gnome.org/Projects/GTK%2B/Inspector > I am also wondering how it is possible to change the button color at runtime? In my opinion i don't think that CSS-based style has alot of advantages over the commonly used object functions. Personally I just add and remove classes as needed: getStyleContext().addClass() getStyleContext().removeClass() |
June 15, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to captaindet | On Wednesday, 15 June 2016 at 22:05:37 UTC, captaindet wrote:
> but i just wanted to mention (in case you are not aware of it) that the CSS can be embedded into the D source. this is what i did to fix GTKs terrible design mistake for the background of Notebook:
What a great tip, I never knew you could do that.
|
June 16, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerald | On Wednesday, 15 June 2016 at 22:34:05 UTC, Gerald wrote: > snip... > >> The text color is green but the button background color is still default-gray! > > I don't see an obvious issue with your code, I usually use CSS classes personally and I know that works fine because I use this technique all over terminix. I would suggest using the GTK Inspector to debug the CSS issue, it's an awesome tool for figuring out GTK CSS issues as it let's you change CSS on the fly, see what CSS is being applied to an object, etc. You can see how to use it at the link below: > > https://wiki.gnome.org/Projects/GTK%2B/Inspector > Do you know if this works on windows? > Personally I just add and remove classes as needed: > > getStyleContext().addClass() > getStyleContext().removeClass() So you basically have to create 2 classes? And what would you do if you would have to change the color randomly (for a simon says game)? I still think it is a bad idea to claim the way with function calls as deprecated but introducing a new system which is not as flexible (but maybe more powerfull). C# with Visual Studio does it, PyQT does it: Function calls. |
June 16, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Thursday, 16 June 2016 at 07:58:56 UTC, TheDGuy wrote: > On Wednesday, 15 June 2016 at 22:34:05 UTC, Gerald wrote: >> snip... >> >>> The text color is green but the button background color is still default-gray! >> >> I don't see an obvious issue with your code, I usually use CSS classes personally and I know that works fine because I use this technique all over terminix. I would suggest using the GTK Inspector to debug the CSS issue, it's an awesome tool for figuring out GTK CSS issues as it let's you change CSS on the fly, see what CSS is being applied to an object, etc. You can see how to use it at the link below: >> >> https://wiki.gnome.org/Projects/GTK%2B/Inspector >> > > Do you know if this works on windows? No idea, I don't use Windows. > >> Personally I just add and remove classes as needed: >> >> getStyleContext().addClass() >> getStyleContext().removeClass() > > So you basically have to create 2 classes? And what would you do if you would have to change the color randomly (for a simon says game)? I still think it is a bad idea to claim the way with function calls as deprecated but introducing a new system which is not as flexible (but maybe more powerfull). > C# with Visual Studio does it, PyQT does it: Function calls. It can be done fine with on the fly changes, i.e. random colors, it's somewhat more work then just calling a simple function call but CSS gives you a lot more power as well. I do this in Terminix where for certain themes I want to set the scrollbar background to be the same color as the terminal background. Essentially, add a class to the widget and then construct the CSS for the class with your random background color as a string. Create a CSSProvider and use loadFromData, same as captaindet's example, to load the CSS in the string. Finally, use the widget's style context to add the CSS provider which you just constructed. If you want to change the color, remove that provider and add a new one. |
June 16, 2016 Re: GTKD - overrideBackgroundColor of Button doesn't work | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerald | On Thursday, 16 June 2016 at 13:12:12 UTC, Gerald wrote: > > It can be done fine with on the fly changes, i.e. random colors, it's somewhat more work then just calling a simple function call but CSS gives you a lot more power as well. I do this in Terminix where for certain themes I want to set the scrollbar background to be the same color as the terminal background. > > Essentially, add a class to the widget and then construct the CSS for the class with your random background color as a string. Create a CSSProvider and use loadFromData, same as captaindet's example, to load the CSS in the string. Finally, use the widget's style context to add the CSS provider which you just constructed. If you want to change the color, remove that provider and add a new one. Ahhh. Thas possible for sure, thanks alot! I finally found the solution for my background-color problem: we have to remove the image first, so this works for me now: #CssName{ background-image: none; background-color:green; color:green; } |
Copyright © 1999-2021 by the D Language Foundation