For a small timer app I want to write a GTK interface where I can set the desired time. Here is a picture of the interface:

However, I am having trouble reading out the fields of the spin buttons. My envisaged procedure for this is the following:
- Read out the buttons using methods for each button
Here is one of the methods that does this:
# Get the fields of the spinbuttons
def get_seconds(self, widget, spin):
self.rSeconds = spin.get_value_as_int()
It is then called like this:
button = gtk.Button("Start")
button.connect("clicked", self.get_seconds, spinnerS)
- Create a timer object with the data from the buttons
This is planned to be accomplished using this method:
# Create the timer object ...
def prepare_timer(self, widget, hours, minutes, seconds, title, text):
self.timer = eggTimer(hours, minutes, seconds, title, text)
Which is called here:
button.connect("clicked", self.prepare_timer, self.rHours, self.rMinutes, self.rSeconds, "some title", "some text")
Unfortunately, when running the script I get the following error message:
Traceback (most recent call last):
File "GTKInterface.py", line 140, in <module>
SpinButtonExample()
File "GTKInterface.py", line 126, in __init__
button.connect("clicked", self.prepare_timer, self.rHours, self.rMinutes, self.rSeconds, "Title", "Text")
AttributeError: SpinButtonExample instance has no attribute 'rSeconds'
To check whether there really is no instance of that variable, I programmed a short method to print it:
def returnS(self, widget):
print self.rSeconds
And surprisingly this method can “see” self.rSeconds. This makes me wonder what determines the visibility of the variable. What am I doing wrong to read this out?
You try to pass the attribute
self.rHoursto theconnectmethod, but at that point the attribute doesn’t exist yet (theclickedhandlers haven’t executed yet).Note that even if you fill in
self.rHoursbefore calling connect, it will pass the value at the time of connecting, not at the time of the handler executing.You can solve this by passing
self.rHoursetc directly toeggTimerinprepare_timer.But it would be even easier to just combine all the click handlers into one, and use local variables instead of
self.rHoursetc. There’s no reason to split your code over many click handlers like this.Edit: BTW, you can also use nested functions instead of methods:
Keep it simple!