On Fri, 14 Nov 2003 17:05:47 +0100
Felix K�hling <[EMAIL PROTECTED]> wrote:

> Hi pygtk experts,
> 
> I want to modify the style of a widget temporarily and reset its
> default style later. I use modify_text to changed the text color of an
> entry. How can I reset it later? According to the gtk reference
> gtk_widget_modify_text(entry, state, NULL) will reset the color to the
> theme's default. However entry.modify_text(state, None) doesn't work.
> It complains that None is not a color. Using set_style(None) as
> suggested in the PythonGtk Tutorial doesn't work either. Styles set
> with modify_* aren't affected by this. Am I missing something or is
> the only way to do this in PyGTK to remember the default color
> somewhere before I modify it?

it looks that way. and, even if the task seems simple, I too didn't find
a handy function or set of functions in the docs.

most surprisingly: executing gtk.Widget.set_style() malfunctions
gtk.Widget.modify_text().

now with some digging and fiddling I found an accaptably elegant
solution - a working example is attached.


walter

#!/usr/bin/env python

try:
    import pygtk; pygtk.require("2.0")
except:
    pass
import gtk

class StyleExample:
    
    def changeTextColor(self, entry, color):
        entry.modify_text(gtk.STATE_NORMAL, color)
        
    def __init__(self):
    
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", gtk.mainquit)
        
        vbox = gtk.VBox()
        window.add(vbox)
        
        entry = gtk.Entry(50)
        entry.set_text("string")
        vbox.add(entry)
        
        # an gtk.Entry has a gtk.Style --> get it with (style =) entry.get_style()
        # a gtk.Style has a "text"-attribute (read only) --> get it with (text =) 
style.text
        # that "text" is an array of gtk.gdk.Colors (with a __getitem__ method)
        # --> get your color with text[0]
        default_color = entry.get_style().text[0]
        changed_color = gtk.gdk.Color(red=50000, green=0, blue=0, pixel=0)
        
        button = gtk.Button("change style")
        button.connect_object("clicked", self.changeTextColor, entry, changed_color)
        vbox.add(button)
        
        button = gtk.Button("reset style")
        button.connect_object("clicked", self.changeTextColor, entry, default_color)
        vbox.add(button)
        
        window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    StyleExample()
    main()

_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to