在python中使用键盘和多线程更新变量



我有一个结构如下的程序:

variable_to_update = 1000

while True: 
# this must be running in an infinite loop
do_something(variable_to_update)

在某些特定时刻,我需要用键盘更新变量variable_to_update

例如:

if key_pressed == 'up':
variable_to_update += 10

有办法做到这一点吗????也许是Threading模块?

如果给定以下程序

variable_to_update = 1000

while True: 
# this must be running in an infinite loop
do_something(variable_to_update)

然后你可以让variable_to_update成为一个列表,并将其传递给函数

variable_to_update = [1000]

while True: 
# this must be running in an infinite loop
do_something(variable_to_update)

和内部函数do_something

def do_something(variable):
variable[0] = "New Value"
print(variable[0])

我知道以上不是最好的方法,但可以作为的变通方法

最新更新