0

在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 循环中调用打印预设时,我得到了正确的结果,列表每次迭代都会增长。

但是在 on_preset_button 我得到空列表。我以为我正在将对预设的引用传递给函数 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

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

4

1 回答 1

0

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

于 2013-07-07T15:11:19.820 回答