GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 20, 2014 4:52 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Can't change GtkButton bg color with Python (GTK+3)
PostPosted: Tue Feb 19, 2013 9:27 pm 
Offline

Joined: Tue Feb 19, 2013 5:46 pm
Posts: 4
(Excuse my english)
Hello! I'm new in the forum.
I'm a Python programmer and until a few days I was using GTK+2. Now, I'm learning GTK+3.
I like the idea to work with css files for styles.
My problem is that I can't change the background color of a GtkButton. I tryed a few examples I found on the web, but none work.

This is the code of one of them:
Code:
from gi.repository import Gtk, Gdk

class MainWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        vbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.VERTICAL)
        self.add(vbox)

        self.entries = [ Gtk.Entry() for i in range(3) ]
        for e in self.entries:
            vbox.pack_start(e, True, True, 0)
            e.connect("changed", self.on_entry_changed)
            e.set_text('123')

        button=Gtk.Button('ok',name='ok-button')
        vbox.pack_end(button,True,True,0)

    def on_entry_changed(self,entry):
        ctx = entry.get_style_context()
        if not entry.get_text().isnumeric():
            ctx.add_class('invalid')
        else:
            ctx.remove_class('invalid')


cssProvider = Gtk.CssProvider()
cssProvider.load_from_path('style.css')
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, cssProvider,
                                     Gtk.STYLE_PROVIDER_PRIORITY_USER)

window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

and the style.css
Code:
GtkEntry.invalid {
    background-color: #ffaaaa;
    background: #ffaaaa;
}

GtkButton#ok-button {
    background-color: green;
    background: green;
}

The entries works well... the bg color change. But the Button no, and theres no error messages.
I tryed also with
Code:
GtkButton {
    background-color: green;
    background: green;
}


I'm only can change the label properties with a css like this
Code:
GtkButton GtkLabe {
    etc...
}


I'm using Python 3.2.3 and Kubuntu 12.10.

Thanks and greetings!


Top
 Profile  
 
 Post subject: Re: Can't change GtkButton bg color with Python (GTK+3)
PostPosted: Wed Feb 20, 2013 7:16 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
colors are controlled by your theme. You should not change it for fun. But if you must, check GtkCssProvider


Top
 Profile  
 
 Post subject: Re: Can't change GtkButton bg color with Python (GTK+3)
PostPosted: Wed Feb 20, 2013 10:50 pm 
Offline

Joined: Tue Feb 19, 2013 5:46 pm
Posts: 4
Thank you rudra!

The code I posted use GtkCssProvider (the same in all the examples I tryed), but don't work for GtkButtons.


Top
 Profile  
 
 Post subject: Re: Can't change GtkButton bg color with Python (GTK+3)
PostPosted: Thu Feb 21, 2013 9:30 pm 
Offline

Joined: Tue Feb 19, 2013 5:46 pm
Posts: 4
I tryed a C code posted in this forum (viewtopic.php?f=3&t=988) that change the button color to blue and don't work neither.
Any help?


Top
 Profile  
 
 Post subject: Re: Can't change GtkButton bg color with Python (GTK+3)
PostPosted: Fri Feb 22, 2013 6:19 pm 
Offline

Joined: Tue Feb 19, 2013 5:46 pm
Posts: 4
Like rudra said, a user in stackoverflow, answer this question saying that is not a good practice to change buttons color. Please check the question in this link http://stackoverflow.com/questions/14983385/why-css-style-dont-work-on-gtkbutton
So, what if I want to create a menu like the one in this image http://c.fsdn.com/con/app/proj/scrmopenerp/screenshots/ProjetatyScrmOpenerp_2.png (see red box)? Wich is the best prectice?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group