You are not logged in.

#1 2012-12-21 22:06:34

android_808
Member
Registered: 2011-10-17
Posts: 17

[SOLVED]Python Gobject ColorButton usage

I'm trying to get a Gtk.ColorButton instance to work but without much luck.

There is a tutorial here for python2, python3 only needs the print statement changing to new syntax:
http://developer.gnome.org/gnome-devel- … py.html.en

Unfortunately this doesn't work, it fails at:

self.button.get_rgba().to_string()

It complains that get_rgba requires two parameters but is only provided with one.  The color class is depreciated, all new apps should be using Gtk.RGBA, which I'm trying to.  Even one of the RGBA functions has a depreciation warning in the C API, suggesting a ColorChooser method be used, but as far as I can tell, ColorChooser is not implemented in the GObject wrapper.

Anyone got any ideas where to start?

Last edited by android_808 (2013-01-12 11:16:09)

Offline

#2 2012-12-23 17:15:09

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,172

Re: [SOLVED]Python Gobject ColorButton usage

From the documentation the get_rgba() function uses a variable passed as a parameter to return the chosen color.
So this parameter needs to be passed. The example code works with these modifications:

from

   # if a new color is chosen, we print it as rgb(r,g,b) in the terminal
    def on_color_chosen(self, user_data):
        print "You chose the color: " + self.button.get_rgba().to_string()

to

    # if a new color is chosen, we print it as rgb(r,g,b) in the terminal
    def on_color_chosen(self, user_data):
        self.button.get_rgba(color)
        print "You chose the color: " + color.to_string()

The 'color' parameter should be declared before it's used, as a global or a local variable.
For example as a global variable:

from

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

to

color = Gdk.RGBA()
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

Edit: correction about the "color" parameter which can be global or local.

Last edited by berbae (2012-12-26 22:33:44)

Offline

#3 2012-12-24 13:16:58

android_808
Member
Registered: 2011-10-17
Posts: 17

Re: [SOLVED]Python Gobject ColorButton usage

i solved it using a local variable before reading your post. evidently i had a mix of set_color with get_rgba which didn't go well.

do you have a link to where you found the docs, none of the information i found related to the current versions?

turns out there is also a problem in conky-colors I need to fix now to get it to work correctly

Offline

#4 2012-12-24 23:06:42

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,172

Re: [SOLVED]Python Gobject ColorButton usage

I found the documentation from two sources:
the C language Gtk+ 3 Reference Manual,
the '/usr/share/gir-1.0/Gtk-3.0.gir' of the introspection scheme.

http://developer.gnome.org/gtk3/stable/ … n-get-rgba gives the 'gtk_color_button_get_rgba ()' C function documentation; it shows that it takes two parameters and the second one is:
rgba : a GdkRGBA to fill in with the current color. [out]

To see how it is introspected into, the Gtk-3.0.gir files shows:

      <method name="get_rgba"
              c:identifier="gtk_color_button_get_rgba"
              version="3.0"
              introspectable="0"
              deprecated="Use gtk_color_chooser_get_rgba() instead."
              deprecated-version="3.4">
        <doc xml:whitespace="preserve">Sets @rgba to be the current color in the #GtkColorButton widget.</doc>
        <return-value transfer-ownership="none">
          <type name="none" c:type="void"/>
        </return-value>
        <parameters>
          <parameter name="rgba"
                     direction="out"
                     caller-allocates="1"
                     transfer-ownership="none">
            <doc xml:whitespace="preserve">a #GdkRGBA to fill in with the current color</doc>
            <type name="Gdk.RGBA" c:type="GdkRGBA*"/>
          </parameter>
        </parameters>
      </method>

it is a method called 'get_rgba' of the ColorButton class, and it takes one parameter of Gdk.RGBA type to give the color (direction="out").

Can you show, please, your solution with a local variable;
and what problem (python scripting?) you have with conky-colors.

Offline

#5 2012-12-26 19:52:25

android_808
Member
Registered: 2011-10-17
Posts: 17

Re: [SOLVED]Python Gobject ColorButton usage

After several days of poor wifi signal and constant drop outs I can finally post a reply!

The code I have used is listed below, where "button" is the Gtk.ColorButton object.  The function returns a 6 character hex representation of the colour.

    def get_colour(self, button):
        colour = Gdk.RGBA()
        button.get_rgba(colour)
        #code = "#"
        code = ""
        for i in (colour.red, colour.green, colour.blue):
            i = hex(int(i*255.0))[2:]
            if len(i) == 1:
                code = code + "0" + i
            else:
                code = code + i
        return code 

The problem with conky-colors I need to check out with helmuthdu, the original creator.  I need to see if the was some reasoning to some decisions or if it is an ommision.  After getting the above code working, I found that several of the parameters for custom themes don't work as you would expect.

  • --default-color is not handled by some of the modes, only tested default and sls at the moment.

  • In theme.c, regardless of the value assigned to --color3, it is reassigned to be the same as --color1.

  • Color4 is not customizable.

Also, please excuse the use of color and colour.  Prefer to try to keep writing in British English where possible to reduce errors in documents.

Offline

#6 2012-12-26 22:45:33

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,172

Re: [SOLVED]Python Gobject ColorButton usage

Thanks for your reply.
I corrected my post #2 to reflect that the parameter passed to get_rgba() needs not to be global.

As I am neither American nor British, using 'color' or 'colour' is not a problem for me (in French it is 'couleur').

Happy new year 2013.

Offline

Board footer

Powered by FluxBB