簡體   English   中英

Python,按任意鍵退出

[英]Python, Press Any Key To Exit

所以,正如標題所說,我想要一個正確的代碼來關閉我的 python 腳本。 到目前為止,我已經使用了input('Press Any Key To Exit') ,但是這樣做會產生錯誤。 我想要一個只關閉你的腳本而不使用錯誤的代碼。

有人有想法嗎? 谷歌給了我輸入選項,但我不希望它使用這個錯誤關閉:

Traceback (most recent call last):
  File "C:/Python27/test", line 1, in <module>
    input('Press Any Key To Exit')
  File "<string>", line 0

   ^
SyntaxError: unexpected EOF while parsing

如果您在 Windows 上,那么 cmd pause命令應該可以工作,盡管它顯示為“按任意鍵繼續”

import os
os.system('pause')

read linux 替代方案,可在此處找到很好的說明

此語法錯誤是由在 Python 2 上使用input引起的,它將嘗試eval在終端提示符下輸入的任何內容。 如果您按下了Enter鍵,那么 Python 基本上會嘗試求值一個空字符串eval("") ,這會導致SyntaxError而不是通常的NameError

如果您很高興將“any”鍵作為回車鍵,那么您可以簡單地將其替換為raw_input

raw_input("Press Enter to continue")

請注意,在 Python 3 上raw_input被重命名為input

對於在搜索中發現此問題的用戶,他們真的希望能夠按任意退出提示而不限於使用enter ,您可以考慮使用第 3 方庫進行跨平台解決方案。 我建議輔助庫readchar可與安裝pip install readchar 它適用於 Linux、macOS 和 Windows 以及 Python 2 或 Python 3。

import readchar
print("Press Any Key To Exit")
k = readchar.readchar()

如果您可以避免使用 Python 中的平台特定功能,我會勸阻它們,但您可以使用內置的msvcrt模塊。

from msvcrt import getch

junk = getch() # Assign to a variable just to suppress output. Blocks until key press.

游戲有點晚了,但幾年前我寫了一個庫來做到這一點。 它公開了帶有可定制消息的pause()函數和受此答案啟發的更通用的跨平台getch()函數。

使用pip install py-getch ,並像這樣使用它:

from getch import pause
pause()

這會打印'Press any key to continue . . .' 'Press any key to continue . . .' 默認情況下。 提供自定義消息:

pause('Press Any Key To Exit.')

為方便起見,它還帶有一個變體,可以在一個步驟中調用sys.exit(status)

pause_exit(0, 'Press Any Key To Exit.')

檢查一下

這是一種通過在 *nix 上按任意鍵結束的方法,不顯示鍵也不按 return (一般方法msvcrt Python read a single character from the user 。)從 SO msvcrt ,似乎您可以使用msvcrt模塊在 Windows 上復制此功能,但我沒有將其安裝在任何地方進行測試. 過度評論以解釋發生了什么......

import sys, termios, tty

stdinFileDesc = sys.stdin.fileno() #store stdin's file descriptor
oldStdinTtyAttr = termios.tcgetattr(stdinFileDesc) #save stdin's tty attributes so I can reset it later

try:
    print 'Press any key to exit...'
    tty.setraw(stdinFileDesc) #set the input mode of stdin so that it gets added to char by char rather than line by line
    sys.stdin.read(1) #read 1 byte from stdin (indicating that a key has been pressed)
finally:
    termios.tcsetattr(stdinFileDesc, termios.TCSADRAIN, oldStdinTtyAttr) #reset stdin to its normal behavior
    print 'Goodbye!'
a = input('Press a key to exit')
if a:
    exit(0)

好的,我在 Linux Mint 17.1 "Rebecca" 上,我似乎已經弄清楚了,正如您可能知道 Linux Mint 安裝了 Python,您無法更新它,也不能在其上安裝另一個版本。 我發現 Linux Mint 中預裝的 python 是 2.7.6 版,因此以下內容肯定適用於 2.7.6 版。 如果你添加raw_input('Press any key to exit')它不會顯示任何錯誤代碼,但它會告訴你程序以代碼 0 退出。例如,這是我的第一個程序。 我的第一個程序 請記住,這是我的第一個程序,我知道它很糟糕,但它是如何使用“按任意鍵退出”的一個很好的例子 順便說一句,這也是我在這個網站上的第一篇文章,很抱歉,如果我格式錯誤。

在 Windows 中:

if msvcrt.kbhit():
    if msvcrt.getch() == b'q':
        exit()

對於 Py3,只需使用: inp = input()

在 python 3 中:

  while True: 
        #runtime..
        answer = input("ENTER something to quit: ") 
        if answer: 
            break 

您可以使用以下代碼:

from os import system as console

console("@echo off&cls")

#Your main code code

print("Press Any Key To Exit")
console("pause>NUL&exit")

據我所知,沒有辦法“按任意鍵”。 input 和 raw_input 命令要求您按 ENTER 鍵。 (Python 3.x 不支持 raw_input)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM