我想在Python中做一個簡單的IRC客戶端(當我學習語言時,作爲一種項目)。Python無阻塞控制檯輸入
我有一個循環,我用來接收和解析什麼IRC服務器發送給我,但如果我使用raw_input
輸入的東西,它停止死循環直到我輸入的東西(顯然)。
如何在不停止循環的情況下輸入內容?
在此先感謝。
(我不認爲我需要張貼代碼,我只是想輸入的東西,而不而1循環停止。)
編輯:我在Windows上。
我想在Python中做一個簡單的IRC客戶端(當我學習語言時,作爲一種項目)。Python無阻塞控制檯輸入
我有一個循環,我用來接收和解析什麼IRC服務器發送給我,但如果我使用raw_input
輸入的東西,它停止死循環直到我輸入的東西(顯然)。
如何在不停止循環的情況下輸入內容?
在此先感謝。
(我不認爲我需要張貼代碼,我只是想輸入的東西,而不而1循環停止。)
編輯:我在Windows上。
對於Windows,控制檯只,使用msvcrt
模塊:
import msvcrt
num = 0
done = False
while not done:
print(num)
num += 1
if msvcrt.kbhit():
print "you pressed",msvcrt.getch(),"so now i will quit"
done = True
對於Linux,這article介紹了以下解決方案,它需要termios
模塊:
import sys
import select
import tty
import termios
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
i = 0
while 1:
print(i)
i += 1
if isData():
c = sys.stdin.read(1)
if c == '\x1b': # x1b is ESC
break
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
對於跨平臺的,或如果你想要一個圖形用戶界面,你可以使用Pygame:
import pygame
from pygame.locals import *
def display(str):
text = font.render(str, True, (255, 255, 255), (159, 182, 205))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
screen.blit(text, textRect)
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Python numbers')
screen.fill((159, 182, 205))
font = pygame.font.Font(None, 17)
num = 0
done = False
while not done:
display(str(num))
num += 1
pygame.event.pump()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
done = True
我已經有pygame了,所以我會試試這個。謝謝。 不過,還有其他人有更好的解決方案嗎?我想保持它的控制檯。 – ImTooStupidForThis 2010-03-09 13:05:09
感謝msvcrt的東西。你太棒了。 – ImTooStupidForThis 2010-03-09 13:25:03
很高興能有所幫助,歡迎來到Stackoverflow。 :) – Mizipzor 2010-03-09 13:44:07
在Linux上,這是對mizipzor代碼的重構,使得它更容易一些,以防萬一您必須在多個地方使用此代碼。
import sys
import select
import tty
import termios
class NonBlockingConsole(object):
def __enter__(self):
self.old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
return self
def __exit__(self, type, value, traceback):
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)
def get_data(self):
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
return sys.stdin.read(1)
return False
以下是如何使用此代碼:此代碼將打印一個計數器,該計數器會持續增長,直到您按ESC。
with NonBlockingConsole() as nbc:
i = 0
while 1:
print i
i += 1
if nbc.get_data() == '\x1b': # x1b is ESC
break
使用GNU/Linux:輸入一個字符後仍然需要按回車鍵,但是它仍然有效。至少它是非阻塞的,它通常會返回正常字符(除了特殊鍵,比如escape或backspace,當然沒有鍵碼)。謝謝! – Luc 2015-07-24 16:16:26
這裏,使用一個單獨的線程在Linux和Windows上運行的解決方案:
import sys
import threading
import time
import Queue
def add_input(input_queue):
while True:
input_queue.put(sys.stdin.read(1))
def foobar():
input_queue = Queue.Queue()
input_thread = threading.Thread(target=add_input, args=(input_queue,))
input_thread.daemon = True
input_thread.start()
last_update = time.time()
while True:
if time.time()-last_update>0.5:
sys.stdout.write(".")
last_update = time.time()
if not input_queue.empty():
print "\ninput:", input_queue.get()
foobar()
似乎是在Windows cmd-console和eclipse上都可以使用的唯一解決方案! – 2013-12-17 07:02:15
這是最真棒solution 我見過。如果鏈接粘貼在這裏下山:
#!/usr/bin/env python
'''
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X). Doesn't work
with IDLE.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
import os
# Windows
if os.name == 'nt':
import msvcrt
# Posix (Linux, OS X)
else:
import sys
import termios
import atexit
from select import select
class KBHit:
def __init__(self):
'''Creates a KBHit object that you can call to do various keyboard things.
'''
if os.name == 'nt':
pass
else:
# Save the terminal settings
self.fd = sys.stdin.fileno()
self.new_term = termios.tcgetattr(self.fd)
self.old_term = termios.tcgetattr(self.fd)
# New terminal setting unbuffered
self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
# Support normal-terminal reset at exit
atexit.register(self.set_normal_term)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def getch(self):
''' Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
'''
s = ''
if os.name == 'nt':
return msvcrt.getch().decode('utf-8')
else:
return sys.stdin.read(1)
def getarrow(self):
''' Returns an arrow-key code after kbhit() has been called. Codes are
0 : up
1 : right
2 : down
3 : left
Should not be called in the same program as getch().
'''
if os.name == 'nt':
msvcrt.getch() # skip 0xE0
c = msvcrt.getch()
vals = [72, 77, 80, 75]
else:
c = sys.stdin.read(3)[2]
vals = [65, 67, 66, 68]
return vals.index(ord(c.decode('utf-8')))
def kbhit(self):
''' Returns True if keyboard character was hit, False otherwise.
'''
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
# Test
if __name__ == "__main__":
kb = KBHit()
print('Hit any key, or ESC to exit')
while True:
if kb.kbhit():
c = kb.getch()
if ord(c) == 27: # ESC
break
print(c)
kb.set_normal_term()
通過Simon D. Levy製成,一個compilation of software的一部分,他撰寫並下Gnu Lesser General Public License釋放。
使用python3.3及以上版本,您可以使用asyncio
模塊,如本答案中所述。 您必須重新考慮代碼才能使用asyncio
。 Prompt for user input using python asyncio.create_server instance
我覺得curses庫可以提供幫助。
import curses
import datetime
stdscr = curses.initscr()
curses.noecho()
stdscr.nodelay(1) # set getch() non-blocking
stdscr.addstr(0,0,"Press \"p\" to show count, \"q\" to exit...")
line = 1
try:
while 1:
c = stdscr.getch()
if c == ord('p'):
stdscr.addstr(line,0,"Some text here")
line += 1
elif c == ord('q'): break
"""
Do more things
"""
finally:
curses.endwin()
curses不是便攜式的。 – kfsone 2017-12-22 00:40:27
我用一個特定於Windows的示例更新了我的答案。 – Mizipzor 2010-03-09 13:21:43
你使用什麼網絡模塊?扭曲的,套接字,asyncore? – DevPlayer 2012-06-10 14:54:57