如何将函数处理程序从其他文件导入到 Python/GTK Builder 程序?
我有一个Python脚本,它使用glade文件来定义其UI,并且有很多重复的小部件,每个小部件都用来调整某个活动对象的不同数值属性。由于它是重复的,因此我决定将所有处理程序定义在一个单独的文件中,以实现封装和可读性。以下是一些代码摘录:
主文件:
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()
Handlers.py 文件:
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()
当我运行程序时,GUI 显示正常,但是当我移动滑块时,我收到此错误:
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
我对命名空间和范围有所了解,但我一般来说,我对 Python 和面向对象非常菜鸟,所以我不知道我到底应该做什么。任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您已经完全忘记了
self
参数。改变这个:
即
I think you've forgotten the
self
argument all over the place.i.e. change this:
to
缺少
obj
参数或名称。也许您需要导入某些内容并分配它,或者将其添加到处理函数的参数中?obj
到底应该是什么?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 theobj
supposed to be?