如何在 Python 3 中立即更新函数内的全局变量?
How to update instantly a global variable inside a function in Python 3?
我有两个全局变量 (StringVar()
) 需要在函数内立即更新,而不是在函数结束时。这是我的代码。
def send_command_onLED():
global ReactionTime_HHHH
global ReactionTime_T
global string_time
global mode
global mode_sel
global command
global text_mode_sel
global go_text
if mode==0:
if command=='GUI':
text_mode_sel.set("") # bisogna fare in modo di far eseguire subito questi due comandi
go_text.set("GO!!!") #
elif command=='CMDL':
print("Ready, steady, go!!!")
ReactionTime_T = ''
ReactionTime_HHHH = ''
numcasuale = random.randint(10,20)
time.sleep(numcasuale)
ser.write("L1".encode())
t0 = time.time()
while(ReactionTime_T!='T'):
ReactionTime_T=ser.read(1).decode('utf-8')
t1 = time.time()
if t1-t0>70:
print("Error TIMEOUT!")
exit()
else:
ReactionTime_HHHH=ser.read(4).decode('utf-8')
ser.reset_input_buffer()
if command=='GUI':
if ReactionTime_HHHH=="FFFF":
string_time.set("You are drunk my friend!!!!")
else:
string_time.set(str(int(ReactionTime_HHHH, 16)) + " ms")
updateScore()
go_text.set("")
elif command=='CMDL':
if ReactionTime_HHHH=="FFFF":
print("\nYou are drunk my friend!!!!\n")
else:
print("\nNew score: " + str(int(ReactionTime_HHHH, 16)) + " ms\n")
mode=1
mode_sel=1
else:
mode_sel=0
if command=='CMDL':
print("Unable to execute this command, the LED is already ON! ")
变量 text_mode_sel
和 go_text
必须在 time.sleep()
之前更新,因为它们是两个标签的文本变量(我使用的是 tkinter)并且它们的变化应该在外部立即看到结束前的功能。换句话说,他们的变化应该立即显示出来。使用我的代码,它们仅在函数结束时更新,并且 time.sleep()
之前的更改没有预期的效果。有没有办法在函数执行期间更新这些变量??
在这种情况下,无需将 text_mode_sel
和 go_text
标记为全局。
从外观上看,在这种情况下只有 mode
和 mode_sel
需要是全局的。其余部分不在该函数中 declared/assigned,因此它们将在没有全局的情况下按预期工作。
至于没有立即更新,在不知道 go_text
和 text_mode_sel
是什么的情况下很难说出您的代码发生了什么,但此代码中没有延迟。我希望您使用的库可能需要重绘命令。
我假设您正在使用 tkinter
如果是,则应调用 root.update()
(或调用任何 tkinter.Tk
实例),正如其名称所示,立即更新屏幕。来自 __doc__
:
Enter event loop until all pending events have been processed by Tcl.
要延迟执行,您可能还需要查看 root.after(time_in_ms, function)
(同样,您的 tkinter.Tk
可能有不同的名称。
我有两个全局变量 (StringVar()
) 需要在函数内立即更新,而不是在函数结束时。这是我的代码。
def send_command_onLED():
global ReactionTime_HHHH
global ReactionTime_T
global string_time
global mode
global mode_sel
global command
global text_mode_sel
global go_text
if mode==0:
if command=='GUI':
text_mode_sel.set("") # bisogna fare in modo di far eseguire subito questi due comandi
go_text.set("GO!!!") #
elif command=='CMDL':
print("Ready, steady, go!!!")
ReactionTime_T = ''
ReactionTime_HHHH = ''
numcasuale = random.randint(10,20)
time.sleep(numcasuale)
ser.write("L1".encode())
t0 = time.time()
while(ReactionTime_T!='T'):
ReactionTime_T=ser.read(1).decode('utf-8')
t1 = time.time()
if t1-t0>70:
print("Error TIMEOUT!")
exit()
else:
ReactionTime_HHHH=ser.read(4).decode('utf-8')
ser.reset_input_buffer()
if command=='GUI':
if ReactionTime_HHHH=="FFFF":
string_time.set("You are drunk my friend!!!!")
else:
string_time.set(str(int(ReactionTime_HHHH, 16)) + " ms")
updateScore()
go_text.set("")
elif command=='CMDL':
if ReactionTime_HHHH=="FFFF":
print("\nYou are drunk my friend!!!!\n")
else:
print("\nNew score: " + str(int(ReactionTime_HHHH, 16)) + " ms\n")
mode=1
mode_sel=1
else:
mode_sel=0
if command=='CMDL':
print("Unable to execute this command, the LED is already ON! ")
变量 text_mode_sel
和 go_text
必须在 time.sleep()
之前更新,因为它们是两个标签的文本变量(我使用的是 tkinter)并且它们的变化应该在外部立即看到结束前的功能。换句话说,他们的变化应该立即显示出来。使用我的代码,它们仅在函数结束时更新,并且 time.sleep()
之前的更改没有预期的效果。有没有办法在函数执行期间更新这些变量??
在这种情况下,无需将 text_mode_sel
和 go_text
标记为全局。
从外观上看,在这种情况下只有 mode
和 mode_sel
需要是全局的。其余部分不在该函数中 declared/assigned,因此它们将在没有全局的情况下按预期工作。
至于没有立即更新,在不知道 go_text
和 text_mode_sel
是什么的情况下很难说出您的代码发生了什么,但此代码中没有延迟。我希望您使用的库可能需要重绘命令。
我假设您正在使用 tkinter
如果是,则应调用 root.update()
(或调用任何 tkinter.Tk
实例),正如其名称所示,立即更新屏幕。来自 __doc__
:
Enter event loop until all pending events have been processed by Tcl.
要延迟执行,您可能还需要查看 root.after(time_in_ms, function)
(同样,您的 tkinter.Tk
可能有不同的名称。