views:

34

answers:

2

I have a Python script which uses a glade file to define its UI, and has a lot of repetitive widgets, each one to adjust a different numerical attribute of a certain active object. Since it is repetitive, I decided to define all the handlers in a separate file for encapsulation and readability. Here are some code excerpts:

The main file:

import pygtk
pygtk.require('2.0')
import gtk, gobject, cairo, gtk.glade

from Handlers import Handlers
from FramesetParameters import FramesetParameters
from GeometricRules import GeometricRules
from BikeDrawing import BikeDrawing

p=FramesetParameters("fitting", "handling", "construction")

builder = gtk.Builder()
builder.add_from_file("FramesetDesignerUI.glade")
Handlers(p)
builder.connect_signals(Handlers.__dict__)

mainWindow = builder.get_object("mainWindow")    
mainWindow.show_all()
gtk.main()

The Handlers.py file:

class Handlers:
    def adjustbottomBracketHeight(widget):
        obj.bottomBracketHeight = widget.get_value()

    def adjustseatTubeAngle(widget):
        obj.seatTubeAngle = widget.get_value()

    def adjustseatTubeLength(widget):
        obj.seatTubeLength = widget.get_value()

    def adjusttopTubeLength(widget):
        obj.topTubeLength = widget.get_value()

    def adjustheadTubeAngle(widget):
        obj.headTubeAngle = widget.get_value()

    def adjustheadTubeTopHeight(widget):
        obj.headTubeTopHeight = widget.get_value()

    def adjustrearAxlePosition(widget):
        obj.rearAxlePosition = widget.get_value()

    def adjusttrail(widget):
        obj.trail = widget.get_value()

    def adjustseatTubeExtension(widget):
        obj.seatTubeExtension = widget.get_value()

    def adjustheadTubeUpperExtension(widget):
        obj.headTubeUpperExtension = widget.get_value()

    def adjustheadTubeLowerExtension(widget):
        obj.headTubeLowerExtension = widget.get_value()

    def adjustforkCrownBulk(widget):
        obj.forkCrownBulk = widget.get_value()

When I run the program, the GUI shows up properly, but when I move a slider I get this error:

Traceback (most recent call last):
  File "/home/helton/Dropbox/Profilez/00Computacional/00REFACTORY97/Handlers.py", line 6, in adjustseatTubeAngle
    obj.seatTubeAngle = widget.get_value()
NameError: global name 'obj' is not defined

I know a little about namespaces and scope, but I am very noob on Python and Object orienting in general, so I do not know exactely what I should do. Any help would be much appreciated.

A: 

the obj arguments or names are missing. perhaps you need to import something and assign it, or add it to the arguments for the handler functions? what exactly is the obj supposed to be?

Blazer
I think I could have made some misconception by using "obj" on function definitions. In fact, the functions are supposed to alter, via widget, the value of some attribute of an existing object of a certain class. In this case, it uses a Scale (slider) to alter the "length" of some line segment to be plotted in a canvas. So, in the "older" version of the program, I defined the handlers inside the main file, and everything worked. But since I have defined these functions in other file, I think there is some problem with scope.
heltonbiker
One downside to Python programming is the fact that there are no `namespace` statements, sadly, so names you declared in your main file won't be directly accessible by other files that are supposed to be in the same 'namespace'. If you cannot add the 'obj' reference as an argument, then there probably isn't any way I know of that you could have the handlers class in a different file
Blazer
A: 

I think you've forgotten the self argument all over the place.

i.e. change this:

class Handlers:
    def adjustbottomBracketHeight(widget):
        obj.bottomBracketHeight = widget.get_value()

to

class Handlers:
    def adjustbottomBracketHeight(obj, widget):
        obj.bottomBracketHeight = widget.get_value()
André Caron
Note that, according to Python conventions, `obj` should be called `self` (the equivalent of `this` in C++ and Java and others).
André Caron
I made the changes you sugested, but now the error is that I am passing just one argument when the function expects two. Besides that, "obj" does not refer to self, since these are callbacks (signal handlers), not methods, or am I missing something? (confused... 8o(
heltonbiker
If the callbacks are not meant to be methods, then you should probably mark them as static with the `@staticmethod` decorator. Did you paste this code from somewhere? What is `obj` supposed to be?
André Caron