当我在控制台窗口中单击时,暂停打印python代码段,如果按下Enter键等键,它将继续打印。这使我的GUI程序也在控制台中打印出一些没有响应的东西,然后我必须按下该控制台中的一个键。如何解决?
import time
import random
running = True
while running:
try:
r = random.randint(1, 100)
print 'hello %s' % r
time.sleep(2)
except KeyboardInterrupt:
running = False
答案 0 :(得分:-2)
import time
import random
import ctypes
win32 = ctypes.windll.kernel32
hin = win32.GetStdHandle(-10)
mode = ctypes.c_int(0)
win32.GetConsoleMode(hin, ctypes.byref(mode))
old_mode = mode.value
# disable Windows console(cmd.exe) quick edit mode
new_mode = old_mode & (~0x0040)
win32.SetConsoleMode(hin, new_mode)
running = True
while running:
try:
r = random.randint(1, 100)
print 'hello %s' % r
time.sleep(1)
except (Exception, KeyboardInterrupt):
win32.SetConsoleMode(hin, old_mode)
running = False