我在函数中调用了全局布尔值,但是即使条件不成立,它仍然会输入“ 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一样。

最佳答案

没有什么可以将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))

关于python - 全局变量不会改变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56460639/

10-12 17:44