Python的乌龟事件监听器全局变量不起作用

时间:2020-06-15 19:05:58

标签: python python-turtle

我有一个项目,每当您按向上箭头时,我都希望更改全局变量size。这是我的代码的一部分:

size = 10

def increase():
    global size
    size += 1
    print("hi")

screen.onkeypress(increase, "Up")

但是当我运行该程序并按向上箭头,然后选中size时,它仍然是10。

知道为什么吗?

感谢您的帮助。 = D

1 个答案:

答案 0 :(得分:0)

您必须包括 listen()方法来记录您的操作。

size = 10

def increase():
    global size
    size += 1
    print("hi")

screen.onkey(increase, "Up")
screen.listen()