Collect value from Gtk Dialog with Python

2

You see, I'm doing an indicator for Ubuntu and I wanted to implement a Preferences window. Since I have no idea about Python, after a lot of effort and a lot of reading of documentation I have managed to create a small window such that:

So far everything is correct. Now I have managed to receive the values of the switch and show them by console as "on" and "off".

Here comes my problem, which I do not know how to define . This is what I have code.

def __init__(self):
    [...]
    label11 = Gtk.Label('Autoarranque:')
    label11.set_alignment(0, 0.5)
    table11.attach(label11, 0, 1, 0, 1, xpadding=5, ypadding=5)
    self.switch1 = Gtk.Switch()
    self.switch1.connect("notify::active", self.on_switch_activated)
    self.switch1.set_active = True
    table11.attach(self.switch1, 1, 2, 0, 1,
                   xpadding=5,
                   ypadding=5,
                   xoptions=Gtk.AttachOptions.SHRINK)
    [...]

def on_switch_activated(self, switch1, gparam):
    if switch1.get_active():
        state = "on"
        return state
    else:
        state = "off"
        return state
    print("Switch ", state)

def close_ok(self):
    stat = on_switch_activated(self, switch1, gparam)
print(stat)

Well, when you throw this, it returns an error within close_ok that says NameError: global name 'on_switch_activated' is not defined . Is not it supposed to be defined? If someone can explain it to me, I would appreciate it.

Greetings!

    
asked by Viral 20.05.2016 в 12:12
source

1 answer

1

What seems to happen to you is that on_switch_activated is a method, not a global function (the class definition is missing). The access to the methods and attributes must be done through the instance, which is normally passed as an argument called self :

def close_ok(self):
    stat = self.on_switch_activated(self.switch1, self.gparam)
    print(stat)

I do not know if gparam was an attribute of the instance or not.

    
answered by 20.05.2016 / 13:19
source