Python整数在循环中表现得很奇怪

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python整数在循环中表现得很奇怪相关的知识,希望对你有一定的参考价值。

循环中的整数表现得很奇怪。每次后来调用整数的时候,整数总是循环中的最后一个,如果绑定的__on_motion被调用,它说i总是23。

        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被调用,它说i总是23.但是我把我的光标移到了另一个有另一个索引的对象上(对不起我的英语)。

答案

我把它固定下来了,用replace every lambda event: ...lambda event, i=i: ...

以上是关于Python整数在循环中表现得很奇怪的主要内容,如果未能解决你的问题,请参考以下文章

EditText表现得很奇怪

SMTP表现得很奇怪

为啥 nodejs 对我的代码表现得很奇怪?

我的 IOS 方向表现得很奇怪,不会听任何编程代码?

UILabel 表现得很奇怪

ReactJS 在渲染组件时表现得很奇怪