2017-05-12 104 views
-1

是否有構建簡單Python命令提示符界面的方法?它只需要輸入文本和輸出內容。構建Python命令提示符界面

到底界面應該看的東西是:

sample

從2017年Pycharm我得到這些錯誤。

Traceback (most recent call last): File "C:/Users/Main/Desktop/Py Sandbox/Keylogger/test.py", line 4, in user_input = prompt('>') File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 548, in prompt eventloop=eventloop) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 576, in run_application output=create_output(true_color=true_color)) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 118, in create_output return Win32Output(stdout) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 80, in init info = self.get_win32_screen_buffer_info() File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 172, in get_win32_screen_buffer_info raise NoConsoleScreenBufferError prompt_toolkit.terminal.win32_output.NoConsoleScreenBufferError: No Windows console found. Are you running cmd.exe?

而且從默認的Python IDLE我得到這個eroor:

Traceback (most recent call last): File "C:/Users/Main/Desktop/adusgisdhui.py", line 4, in user_input = prompt('>') File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 548, in prompt eventloop=eventloop) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 576, in run_application output=create_output(true_color=true_color)) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\shortcuts.py", line 118, in create_output return Win32Output(stdout) File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 80, in init info = self.get_win32_screen_buffer_info() File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 162, in get_win32_screen_buffer_info self.flush() File "C:\Users\Main\AppData\Local\Programs\Python\Python35\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 288, in flush self.stdout.flush() AttributeError: 'NoneType' object has no attribute 'flush'

+1

你不需要一個單獨的模塊。 「輸入」功能是一個內置的功能。 –

+0

[Python:user input and commandline arguments]可能的重複(http://stackoverflow.com/questions/70797/python-user-input-and-commandline-arguments) –

+1

可能的重複[如何在Python中編寫shell ](http://stackoverflow.com/questions/2594560/how-to-write-a-shell-in-python) – KronoS

回答

0

非常基本的代碼

while 1: 
    userinput = input(">>") 

if userinput == "help": 
    print("You asked for help") 
elif ... 
0

下面是關於建立命令行界面爲你的Python程序CLI Python一個偉大的文章。其中一種方法是使用prompt_toolkit

from prompt_toolkit import prompt 

while 1: 
    user_input = prompt('>') 
    print(user_input) 

這一操作將繼續要求用戶輸入。它還具有命令歷史和自動建議等功能。

+0

正是我需要的,但我似乎沒有在我的電腦上運行。 – Cvqe

+0

我的錯誤提到上面的問題,因爲Stackoverflow不允許太長的評論。 – Cvqe