How can I get a character in Python similar to Pascal readkey
In Pascal I can execute this code to get a character from keyboard input:
uses crt;
var ch: char;
begin
ch := '.';
while ch <> '\' do
begin
ch := readkey;
writeln( ch );
end开发者_如何学运维;
end;
Is there a similar one in Python? :)
import sys
def prog():
char = ""
while char != "/":
char = sys.stdin.read(1)
print char
prog()
You could do it by running Tkinter
in the background:
import Tkinter
def keyPress(event, tk):
ch = event.char
if ch == '\\':
tk.destroy()
else:
print ch
if __name__ == '__main__':
tk = Tkinter.Tk()
tk.bind_all('<Key>', lambda event: keyPress(event, tk))
tk.withdraw()
tk.mainloop()
(Hacked from: http://www.daniweb.com/forums/post567365.html#post567365)
raw_input.
Then slice the first character.
You can't use CRT
; I recommend you to import pygame
instead.
精彩评论