Python整数在循环中表现奇怪

时间:2019-06-04 15:29:35

标签: python loops integer

循环中的整数表现得很奇怪:每次以后调用整数时,整数始终是循环中的最后一个。

        for b in info:
            if y > config["height"] - 160:
                if g:
                    f = i
                    self.f = f
                g = False
                y -= 160 * f
                x += 200
                self.x = x
            self.button[i] = canvas.create_rectangle(50 + x, 1 + y, 100 + x, 51 + y, outline="white",
                                                     fill="white")
            self.frame[i] = canvas.create_rectangle(0 + x, 0 + y, 190 + x, 140 + y, outline="#cfcfcf",
                                                    fill="white")
            self.item[i] = canvas.create_image(100 + x, 26 + y, image=icons["store-pack"][i])
            self.name[i] = canvas.create_text(100 + x, 75 + y, text=b["name"], fill="#7f7f7f", anchor=CENTER)
            self.c_icon[i] = canvas.create_image(40 + x, 117 + y, image=icons["store-coin"], anchor=W)
            self.coins[i] = canvas.create_text(95 + x, 117 + y, text=b["coins"], fill="#7f7f7f", font=("helvetica", 18), anchor=CENTER)
            self.items[i] = i
            canvas.tag_bind(self.button[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))
            canvas.tag_bind(self.frame[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))
            canvas.tag_bind(self.item[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))
            canvas.tag_bind(self.name[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))
            canvas.tag_bind(self.c_icon[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))
            canvas.tag_bind(self.coins[i], "<Motion>", lambda event: self.__on_motion(canvas, deepcopy(i)))

            canvas.tag_bind(self.button[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            canvas.tag_bind(self.frame[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            canvas.tag_bind(self.item[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            canvas.tag_bind(self.name[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            canvas.tag_bind(self.c_icon[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            canvas.tag_bind(self.coins[i], "<ButtonPress-1>", lambda event: self.buy_selected(config, modes, root, stats, canvas))
            y += 160
            i += 1

如果调用绑定的__on_motion,则表示我始终为23。 但是我将光标移到了另一个索引的对象上。 (对不起,我的英语)

1 个答案:

答案 0 :(得分:0)

我修复了问题,将每个lambda event: ...替换为lambda event, i=i: ...