新聞中心
如何使用python編寫(xiě)文本菜單

洞頭網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
什么是文本菜單?
簡(jiǎn)單一句話,現(xiàn)在你能看到的都是圖形菜單界面,退后20年,你能看到都是文本菜單界面。
文本菜單界面通常在以前比較老的DOS軟件里見(jiàn)到,例如老的PCTOOLS軟件,現(xiàn)在已經(jīng)不容易找到了。
目前在windows系統(tǒng)下的軟件界面一般都是圖形菜單界面。
如何來(lái)實(shí)現(xiàn)文本菜單式的交互呢?
將menu.py,運(yùn)行python menu.py即可。
menu.py代碼如下:
------menu.py----------
#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Edit: turnipsmart.com
import os,sys
running = True
menu = """
Main Menu
--------------------
1: Display Options
2: Config Options
3: Deteting
h: Help
q: Quit
--------------------
"""
menu_dict={
"h": "Please enter the options to be operated.",
"1": "df -h",
"2": "free -m",
"3": "netstat -lnt",
}
def commands(args):
cmd = menu_dict.get(args)
return cmd
if __name__ == "__main__":
os.system('cls')
print menu
while running:
cmd = raw_input("Input Your Commond:")
if cmd != 'q':
os.system('cls')
try:
print menu
if commands(cmd) != None:
#fo = os.popen(commands(cmd))
#print fo.read()
if cmd == '1':
print "cmd=1"
elif cmd == '2':
print "cmd=2"
elif cmd == '3':
print "cmd=3"
else:
print commands(cmd)
else:
print "Input is Wrong!"
except Exception,e:
print menu
print e
else:
print 'We will exit the menu.'
os.system('cls')
sys.exit()效果如下:
更多技術(shù)請(qǐng)關(guān)注Python視頻教程。
文章題目:創(chuàng)新互聯(lián)Python教程:如何使用Python編寫(xiě)文本菜單
標(biāo)題路徑:http://www.5511xx.com/article/cccoodg.html


咨詢
建站咨詢
