在帕斯卡爾我可以執行這個代碼從鍵盤輸入的字符:我怎樣才能在Python類似於帕斯卡爾字符readkey
uses crt;
var ch: char;
begin
ch := '.';
while ch <> '\' do
begin
ch := readkey;
writeln(ch);
end;
end;
是否有類似的一個在Python? :)
在帕斯卡爾我可以執行這個代碼從鍵盤輸入的字符:我怎樣才能在Python類似於帕斯卡爾字符readkey
uses crt;
var ch: char;
begin
ch := '.';
while ch <> '\' do
begin
ch := readkey;
writeln(ch);
end;
end;
是否有類似的一個在Python? :)
您不能use CRT
;我建議您改爲import pygame
。
您可以通過在後臺運行Tkinter
做到這一點:
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()
(被黑:http://www.daniweb.com/forums/post567365.html#post567365)
import sys
def prog():
char = ""
while char != "/":
char = sys.stdin.read(1)
print char
prog()
http://stackoverflow.com/questions/510357/python-read-單用戶字符 – 6502 2011-01-25 21:49:40