python传递列表引用函数

时间:2013-07-07 14:28:45

标签: python list function arguments glade

在python中的

我在类中有以下代码:

def __init__(self):
    presets = []
    for i in range(6):
        presets.append(dbuilder.get_object("button_pres"+str(i)))
        presets[i].connect("clicked", self.on_preset_button, i, presets)
        print presets

def on_preset_button(self, origin, i, presets):
    recall(i)
    print presets

现在,当我在for循环中调用print预设时,每次迭代都会生成正确的结果。

但是在on_preset_button中我得到了空列表。我以为我将对presets的引用传递给函数connect,而函数connect又将引用传递给on_preset_button。但似乎没有。

我想访问之前附加到预设的6个元素。请帮忙!

这是长版:

import gtk

from galicaster.core import context
from galicaster.utils.visca import recall

class DistribUI(gtk.Box):
    __gtype_name__ = 'DistribUI'

    def __init__(self):
        gtk.Box.__init__(self)
        dbuilder= gtk.Builder()
        dbuilder.add_from_file(get_ui_path('distrib.glade'))
        self.builder = dbuilder

        # other gui elements omitted

        presets = []
        for i in range(6):
            presets.append(dbuilder.get_object("button_pres"+str(i)))
            presets[i].connect("clicked", self.on_preset_button, i, presets)
            print presets

    def on_preset_button(self, origin, i, presets):
            recall(i)
            print presets

这是加载类后的输出:

[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>]
[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>, <gtk.Button object at 0x204ab90 (GtkButton at 0x20ad4b0)>]
[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>, <gtk.Button object at 0x204ab90 (GtkButton at 0x20ad4b0)>, <gtk.Button object at 0x204ac30 (GtkButton at 0x20ad630)>]
[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>, <gtk.Button object at 0x204ab90 (GtkButton at 0x20ad4b0)>, <gtk.Button object at 0x204ac30 (GtkButton at 0x20ad630)>, <gtk.Button object at 0x204acd0 (GtkButton at 0x20ad3f0)>]
[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>, <gtk.Button object at 0x204ab90 (GtkButton at 0x20ad4b0)>, <gtk.Button object at 0x204ac30 (GtkButton at 0x20ad630)>, <gtk.Button object at 0x204acd0 (GtkButton at 0x20ad3f0)>, <gtk.Button object at 0x204ad70 (GtkButton at 0x20ad570)>]
[<gtk.Button object at 0x204ab40 (GtkButton at 0x20ad330)>, <gtk.Button object at 0x204ab90 (GtkButton at 0x20ad4b0)>, <gtk.Button object at 0x204ac30 (GtkButton at 0x20ad630)>, <gtk.Button object at 0x204acd0 (GtkButton at 0x20ad3f0)>, <gtk.Button object at 0x204ad70 (GtkButton at 0x20ad570)>, <gtk.Button object at 0x204ae10 (GtkButton at 0x20ad6f0)>]

以下是点击按钮1后的输出:

[]
10 OK - no return value

最后一行是召回(i)的结果,它是一个外部子流程。就在此之前,您可以看到空列表:(

1 个答案:

答案 0 :(得分:0)

建议你在这里发布你的日志,源代码更好。 听起来像connect函数在调用on_preset_button之前做了一些事情