如何将函数处理程序从其他文件导入到 Python/GTK Builder 程序?

发布于 2024-09-25 04:25:04 字数 2191 浏览 9 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2024-10-02 04:25:04

我认为您已经完全忘记了 self 参数。

改变这个:

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

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

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()
清风夜微凉 2024-10-02 04:25:04

缺少 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 the obj supposed to be?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文