Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
class PreferencesCalorieBurnerDialog(PreferencesDialog):
    __gtype_name__ = "PreferencesCalorieBurnerDialog"

       def finish_initializing(self, builder): # pylint: disable=E1002
           """Set up the preferences dialog"""
           super(PreferencesCalorieBurnerDialog, self).finish_initializing(builder)

           # Bind each preference widget to gsettings
           settings = Gio.Settings("net.launchpad.calorie-burner")
           widget = self.builder.get_object('example_entry')
           settings.bind("example", widget, "text", Gio.SettingsBindFlags.DEFAULT)

           #Custom preference
           widget = self.builder.get_object('weight')
           settings.bind("weight", widget, "float", Gio.SettingsBindFlags.DEFAULT)

Main Dialog

self.PreferencesDialog.get_weight()???

share|improve this question

1 Answer 1

Ok I solved it.

First I had to edit data/glib-2.0/schemas/net.launchpad.calorie-burner.gschema.xml:

Note that type="s" is for strings and type="i" is for integers

<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="calorie-burner">
  <schema id="net.launchpad.calorie-burner" path="/net/launchpad/calorie-burner/">
    <key name="weight" type="i">
      <range min="1" max="350"/>
      <default>100</default>
      <summary>User's Weight Kgs</summary>
    </key>
    <key name="units" type="s">
      <choices>
        <choice value='Metric'/>
        <choice value='Imperial'/>
      </choices>
      <default>'Metric'</default>
    </key>
    <key name="energy" type="s">
      <choices>
        <choice value='KJs'/>
        <choice value='Calories'/>
      </choices>
      <default>'KJs'</default>
    </key>
  </schema>
</schemalist>

Next I had to wire up my ui elements to my preferences:

In the preferences dialog I had to do the following:

    # Bind each preference widget to gsettings
    settings = Gio.Settings("net.launchpad.calorie-burner")

    widget = self.builder.get_object('weight')
    settings.bind("weight", widget, "value", Gio.SettingsBindFlags.DEFAULT)

    widget = self.builder.get_object('cmbUnits-entry')
    settings.bind("units", widget, "text", Gio.SettingsBindFlags.DEFAULT)

    widget = self.builder.get_object('cmbEnergy-entry')
    settings.bind("energy", widget, "text", Gio.SettingsBindFlags.DEFAULT)

Note that the first argument is the name of the setting, the second is the widget on the ui and the thrid is the property of the widget that contains the data I want to store in my preferences.

Also note for comboboxes I had to enable "Has Entry" to create a text box with the text property.

Lastly in my main dialog I had to do the following:

    from gi.repository import Gtk, Gio # pylint: disable=E0611
    self.settings = Gio.Settings("net.launchpad.calorie-burner")

To access the settings call either get_string or get_int with the name of the setting:

  self.settings.get_string("energy")
  self.settings.get_string("units")
  self.settings.get_int("weight")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.