Windows cmd控制台停止打印

时间:2014-09-19 22:21:58

标签: python windows cmd

当我在控制台窗口中单击时,暂停打印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

1 个答案:

答案 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