In the following code I am trying to select a file either from a file chooser or just typing out the path. The problem is I want the comboboxentry to change background color when something is put in it. The code works fine if it's a combobox (Line 7 is 'widget = gtk.combo_box_new_text()'), but not if it's the comboboxentry. An explanation for why this isn't working would be much appreciated.

import gtk

class ComboBoxEntryExample:
    def __init__(self):
        window = gtk.Window()
        window.connect('destroy', lambda w: gtk.main_quit())
        widget = gtk.combo_box_entry_new_text()
        window.add(widget)
        widget.append_text('Select a File:')
        widget.append_text('')
        widget.connect('changed', self.changed_cb)
        window.show_all()

    def open_file(self, widget):
        #"""Opens up a file chooser box for the datasheet path."""
        filechooser = gtk.FileChooserDialog("Open..", None, \
                                            gtk.FILE_CHOOSER_ACTION_OPEN, \
                                   (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))  
        filechooser.set_default_response(gtk.RESPONSE_OK)
        filechooser.set_current_folder('C:\\')
        
        ans = filechooser.run()
        if ans == gtk.RESPONSE_OK:
            #Sets the input to the files directory
            widget.remove_text(1)
            widget.append_text(filechooser.get_filename())
            widget.set_active(1)
        elif ans == gtk.RESPONSE_CANCEL:
            #Clears the entry field
            widget.remove_text(1)
            widget.append_text('')
            widget.set_active(1)
        filechooser.destroy()

    def changed_cb(self, widget):
        index = widget.get_active()

        if (index == 0):
            self.open_file(widget)
        else:
            widget.set_active(index)
            
        widget.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FF6464'))
        

if __name__ == "__main__":
    bcb = ComboBoxEntryExample()
    gtk.main()

For anyone that wanted to know I got it to work by changing line 44 to
widget.child.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FF6464')).
It figures shortly after I ask for help I find the answer. Thanks anyway.

In the following code I am trying to select a file either from a file chooser or just typing out the path. The problem is I want the comboboxentry to change background color when something is put in it. The code works fine if it's a combobox (Line 7 is 'widget = gtk.combo_box_new_text()'), but not if it's the comboboxentry. An explanation for why this isn't working would be much appreciated.

import gtk

class ComboBoxEntryExample:
    def __init__(self):
        window = gtk.Window()
        window.connect('destroy', lambda w: gtk.main_quit())
        widget = gtk.combo_box_entry_new_text()
        window.add(widget)
        widget.append_text('Select a File:')
        widget.append_text('')
        widget.connect('changed', self.changed_cb)
        window.show_all()

    def open_file(self, widget):
        #"""Opens up a file chooser box for the datasheet path."""
        filechooser = gtk.FileChooserDialog("Open..", None, \
                                            gtk.FILE_CHOOSER_ACTION_OPEN, \
                                   (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))  
        filechooser.set_default_response(gtk.RESPONSE_OK)
        filechooser.set_current_folder('C:\\')
        
        ans = filechooser.run()
        if ans == gtk.RESPONSE_OK:
            #Sets the input to the files directory
            widget.remove_text(1)
            widget.append_text(filechooser.get_filename())
            widget.set_active(1)
        elif ans == gtk.RESPONSE_CANCEL:
            #Clears the entry field
            widget.remove_text(1)
            widget.append_text('')
            widget.set_active(1)
        filechooser.destroy()

    def changed_cb(self, widget):
        index = widget.get_active()

        if (index == 0):
            self.open_file(widget)
        else:
            widget.set_active(index)
            
        widget.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FF6464'))
        

if __name__ == "__main__":
    bcb = ComboBoxEntryExample()
    gtk.main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.