简体   繁体   English

全局变量不会改变

[英]Global variable would not change

I called a global boolean in the function, but it still enters the 'if' even though the condition is not true. 我在函数中调用了全局布尔值,但是即使条件不成立,它仍然会输入“ if”。

I've been searching around but couldn't find a similar problem. 我一直在搜索,但是找不到类似的问题。

from Tkinter import *
import time

jumping = False
def jump(parts, high = 25): #to fix: can jump again in middle jump
    global jumping
    jumping = True
    for t in range(high*-3, 0, 1):
        w.move(parts[0], 0, 1)
        master.update()
        time.sleep(.0083)
    jumping = False
#thats the condition, which can be entered even while "jump()" is running.
if not jumping:
    master.bind('w', lambda x: jump(penguin_parts))

I expected it not to allow me to press 'w' and activate "jump()" when the function is in process but it actually lets me, just like jumping is still False. 我希望它不允许我在执行该函数时按下“ w”并激活“ jump()”,但实际上它允许我,就像跳转仍然是False一样。

There's nothing there to unbind the w key to the jump() function. 没有什么可以将w键绑定到jump()函数。 Once it's bound, it remains bound until you unbind it. 绑定之后,它将一直保持绑定状态,直到您取消绑定为止。

from Tkinter import *
import time

jumping = False
def jump(parts, high = 25):
    if jumping == False:
        global jumping
        jumping = True
        for t in range(high*-3, 0, 1):
            w.move(parts[0], 0, 1)
            master.update()
            time.sleep(.0083)
        jumping = False
    master.bind('w', lambda x: jump(penguin_parts))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM