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 = wid开发者_开发技巧get.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.
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()
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?
精彩评论