全局变量不会改变

时间:2019-06-05 12:37:51

标签: python tkinter

我在函数中调用了一个全局布尔值,但是即使条件不成立,它仍然会输入'if'。

我一直在搜索,但是找不到类似的问题。

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))

我希望它不让我在执行该函数时按下“ w”并激活“ jump()”,但实际上它让我,就像跳跃仍然是False。

1 个答案:

答案 0 :(得分:2)

没有任何将w键绑定到jump()函数的操作。绑定之后,它将一直保持绑定状态,直到您取消绑定为止。

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))