本教程的目的是向您介紹在 NetBeans IDE 中使用 Python 編程語(yǔ)言的基礎(chǔ)知識(shí)。這樣,程序員就可以繼續(xù)深入了解該語(yǔ)言的功能。NetBeans 添加了對(duì)多種 JVM 語(yǔ)言和非 JVM 語(yǔ)言的支持,從而為程序員開辟了新的編程途徑,同時(shí)也為他們以多種不同的語(yǔ)言(包括 Python)進(jìn)行開發(fā)提供了易于使用的環(huán)境。在本教程中,您將從頭開發(fā)一個(gè)小型應(yīng)用程序 (hockeyRoster.py)。開發(fā)出的應(yīng)用程序?qū)⒂糜谟涗浐凸芾砬蜿?duì)信息。
預(yù)計(jì)時(shí)間:20 分鐘
目錄
應(yīng)用程序概述
設(shè)置
添加新 Python 安裝
安裝并配置 Python 安裝
創(chuàng)建項(xiàng)目
添加代碼
運(yùn)行應(yīng)用程序
小結(jié)
要學(xué)習(xí)本教程,您需要具備以下軟件和資源。
軟件或資源 | 要求的版本 |
NetBeans IDE | 6.5 或 6.7 Python EA 版本 |
Java Developer Kit (JDK) | 版本 6 或版本 5 |
Python | 2.5.x |
樣例數(shù)據(jù)庫(kù) | 無(wú)要求 |
應(yīng)用程序概述
在本基礎(chǔ)教程中,您將開發(fā)一個(gè)管理曲棍球隊(duì)信息的命令行應(yīng)用程序,通過該應(yīng)用程序可以將包含球員及其首選球場(chǎng)位置和當(dāng)前狀態(tài)("BENCHED" 或 "ACTIVE")的列表添加到外部文件中。這樣,您便可以通過 Python 列出該文件的內(nèi)容,并更新記錄。本教程涉及文件輸入/輸出內(nèi)容,但并未深入介紹有關(guān)數(shù)據(jù)庫(kù)的詳細(xì)信息。
設(shè)置
需要下載 NetBeans IDE 的 Python EA2 版本才能學(xué)完本教程。
注意:如果使用的是 NetBeans IDE 6.8,可通過以下操作從 Beta 更新中心安裝 Python 插件:
1. 在 IDE 的主菜單中,轉(zhuǎn)至“工具”>“插件”。
2. 在“插件”對(duì)話框中,單擊“可用插件”標(biāo)簽并在“類別”列中查找 Python。
3. 選中 Python 和 Jython 發(fā)行版本插件旁邊的“安裝”框,然后單擊“安裝”。
4. 在“NetBeans IDE 安裝程序”對(duì)話框中,確認(rèn)將要安裝 Python 和 Jython 插件,單擊“下一步”,然后接受許可證,安裝開始。
您可以在 "Python Platform Manager"(Python 平臺(tái)管理器)中配置 IDE 以支持 Python。此窗口允許您添加或刪除可用于 IDE 的 Python 安裝。使用 "Python Platform Manager"(Python 平臺(tái)管理器),還可以設(shè)置和配置 Python 路徑及 Java 路徑。通過執(zhí)行上述操作,可以將 Java jar 文件或 Python 模塊置于 Path 變量中,以便在每次啟動(dòng) IDE 時(shí)都可以使用這些文件或模塊。此外,您甚至還可以為所配置的每個(gè)安裝定義不同的 Java 路徑或 Python 路徑。
添加新 Python 安裝
1. 在主工具欄中,選擇“工具”> "Python Platforms Manager"(Python 平臺(tái)管理器)。
此時(shí)會(huì)打開 "Python Platform Manager"(Python 平臺(tái)管理器),如下圖所示。
2. 如果缺省情況下未顯示 Python 安裝,請(qǐng)單擊 "New"(新建),瀏覽至要使用的安裝,然后選擇“打開”。
IDE 會(huì)將該安裝添加到 "Platforms"(平臺(tái))列表中。如果您不確定安裝的位置,請(qǐng)單擊 "Auto Detect"(自動(dòng)檢測(cè))。
安裝并配置 Python 安裝
1. 選擇“工具”> "Python Installations"(Python 安裝)以打開 "Python Platform Manager"(Python 平臺(tái)管理器)。
2. 選擇要用于 IDE 的 Python 安裝,然后單擊 "Make Default"(設(shè)為缺省)。
3. 在 "Command Arguments"(命令參數(shù))文本字段中,輸入安裝的任何命令參數(shù)。如有必要,也可以對(duì)平臺(tái)重命名。
4. 單擊 "Close"(關(guān)閉)關(guān)閉 "Python Platform Manager"(Python 平臺(tái)管理器)。
創(chuàng)建項(xiàng)目
首先,在 IDE 中創(chuàng)建一個(gè)新 Python 項(xiàng)目?!靶陆?xiàng)目”向?qū)ОS多項(xiàng)目類型的內(nèi)置模板。訪問“新建項(xiàng)目”向?qū)в卸喾N方法??梢栽谥鞴ぞ邫谥羞x擇“文件”>“新建項(xiàng)目”,也可以在“項(xiàng)目”窗口中單擊鼠標(biāo)右鍵,然后選擇“新建項(xiàng)目”。
1. 在主工具欄中,選擇“文件”>“新建項(xiàng)目”。
此時(shí)將打開“新建項(xiàng)目”向?qū)?,其中顯示了 "Python" 類別。
注意:當(dāng)計(jì)算機(jī)中只是安裝了 IDE 的 Python EA 版本時(shí),"Python" 會(huì)顯示為項(xiàng)目類別。如果 Python EA 是作為插件添加到 IDE 中的,則可能會(huì)顯示其他類別。
2. 選擇 "Python Project"(Python 項(xiàng)目)作為項(xiàng)目類型,如下圖所示。單擊“下一步”。
3. 輸入 HockeyRoster 作為項(xiàng)目名稱,如下圖所示。選擇要用于該項(xiàng)目的 Python 版本,然后重命名主文件 HockeyRoster.py。
4. 將 "Set as Main Project"(設(shè)置為主項(xiàng)目)復(fù)選框和 "Create Main File"(創(chuàng)建主文件)復(fù)選框保留為選中狀態(tài)。單擊“完成”。
IDE 將基于項(xiàng)目的名稱來(lái)創(chuàng)建項(xiàng)目文件夾。您可以更改此文件夾的名稱和位置。
5. 從 "Python Platform"(Python 平臺(tái))下拉列表中選擇要使用的 Python 平臺(tái),然后單擊“完成”以創(chuàng)建項(xiàng)目。
IDE 將創(chuàng)建該項(xiàng)目。請(qǐng)注意在“項(xiàng)目”窗口中是如何顯示該項(xiàng)目的。
請(qǐng)注意,在 IDE 的源代碼編輯器中將打開顯示一些基本信息的 HockeyRoster.py 文件,如下圖所示。NetBeans IDE 會(huì)自動(dòng)記錄項(xiàng)目的作者和創(chuàng)建日期,并且還會(huì)提供一個(gè)輸出 "Hello" 的簡(jiǎn)短樣例程序。
6.. 現(xiàn)在,您要為該應(yīng)用程序創(chuàng)建第二個(gè)源文件。在“項(xiàng)目”窗口中,右鍵單擊該項(xiàng)目的 "Sources"(源)節(jié)點(diǎn),然后選擇“新建”> "Empty Module"(空模塊),如下圖所示。
此時(shí)會(huì)打開 "New Empty Module"(新建空模塊)向?qū)А?/p>
7. 鍵入 Player 作為文件名,然后單擊“完成”。
將在源代碼編輯器中打開該文件。此時(shí),“項(xiàng)目”窗口應(yīng)與下圖類似。
添加代碼
在此部分,您要將 Python 代碼添加到在本教程上一部分所創(chuàng)建的文件中。
1. 如果尚未打開 Player.py 文件,請(qǐng)?jiān)谠创a編輯器中將其打開。
2. 在 Player.py 文件中鍵入以下代碼。
- # Player.py
- #
- # Container to hold our player objects
- class Player:
- # Player attributes
- id = 0
- first = None
- last = None
- position = None
- # Function used to create a player object
- def create(self, id, first, last, position):
- self.id = id
- self.first = first
- self.last = last
- self.position = position
此時(shí),源代碼編輯器應(yīng)與下圖類似。請(qǐng)注意 IDE 是如何幫助您輸入括號(hào)和冒號(hào)的。
3. 在源代碼編輯器中打開 HockeyPlayer.py。
4. 刪除 Hockeyplayer.py 中的現(xiàn)有代碼,然后鍵入以下代碼。這是添加的第一段代碼,它主要由注釋組成,但還將執(zhí)行以下兩項(xiàng)重要操作:
◆ 從先前創(chuàng)建的 Player 模塊中導(dǎo)入 Player 類。
◆ 定義一個(gè)列表,以記錄每個(gè) Player 對(duì)象。
- # HockeyRoster.py
- #
- # Implementation logic for the HockeyRoster application
- # Import Player class from the Player module
- from Player import Player
- # Define a list to hold each of the Player objects
- playerList = []
5. 現(xiàn)在,您要向應(yīng)用程序中添加更多代碼。此段代碼將創(chuàng)建一個(gè)用于在“輸出”窗口中生成輸出的函數(shù)(該輸出基于用戶所輸入的內(nèi)容),從而為應(yīng)用程序創(chuàng)建一個(gè)選擇器。
- # makeSelection()
- #
- # Creates a selector for our application. The function prints output to the
- # command line. It then takes a parameter as keyboard input at the command line
- # in order to choose our application option.
- def makeSelection():
- validOptions = ['1','2','3','4']
- print "Please choose an option\n"
- selection = raw_input("Press 1 to add a player, 2 to print the team roster,
- 3 to search for a player on the team, 4 to quit: ")
- if selection not in validOptions:
- print "Not a valid option, please try again\n"
- makeSelection()
- else:
- if selection == '1':
- addPlayer()
- elif selection == '2':
- printRoster()
- elif selection == '3':
- searchRoster()
- else:
- print "Thanks for using the HockeyRoster application."
6. 右鍵單擊源代碼編輯器中的任意位置,然后選擇“格式化代碼”,如下圖所示。
現(xiàn)在,您要添加一些代碼,以接受來(lái)自“輸出”窗口的鍵盤輸入。用戶可以輸入球員的名、姓及場(chǎng)上位置。
- # addPlayer()
- #
- # Accepts keyboard input to add a player object to the roster list. This function
- # creates a new player object each time it is invoked and appends it to the list.
- def addPlayer():
- addNew = 'Y'
- print "Add a player to the roster by providing the following information\n"
- while addNew.upper() == 'Y':
- first = raw_input("First Name: ")
- last = raw_input("Last Name: ")
- position = raw_input("Position: ")
- id = len(playerList)
- player = Player()
- player.create(id, first, last, position)
- playerList.append(player)
- print "Player successfully added to the team roster\n"
- addNew = raw_input("Add another? (Y or N)")
- makeSelection()
請(qǐng)注意 IDE 的代碼完成功能是如何在您鍵入代碼時(shí)提供建議的,如下圖所示。
8. 在此步中,您要添加將列表內(nèi)容以報(bào)告形式輸出到“輸出”窗口的代碼。
- # printRoster()
- #
- # Prints the contents of the list to the command line as a report
- def printRoster():
- print "====================\n"
- print "Complete Team Roster\n"
- print "======================\n\n"
- for player in playerList:
- print "%s %s - %s" % (player.first, player.last, player.position)
- print "\n"
- print "=== End of Roster ===\n"
- makeSelection()
9. 現(xiàn)在,您要輸入一些代碼,以提取從“輸出”窗口輸入的球員姓名,并在花名冊(cè)中搜索與其匹配的內(nèi)容。
- # searchRoster()
- #
- # Takes input from the command line for a player's name to search within the
- # roster list. If the player is found in the list then an affirmative message
- # is printed. If not found, then a negative message is printed.
- def searchRoster():
- index = 0
- found = False
- print "Enter a player name below to search the team\n"
- first = raw_input("First Name: ")
- last = raw_input("Last Name: ")
- position = None
- while index < len(playerList):
- player = playerList[index]
- if player.first.upper() == first.upper() or player.last.upper() == last.upper():
- found = True
- position = player.position
- index = index + 1
- if found:
- print '%s %s is in the roster as %s' % (first, last, position)
- else:
- print '%s %s is not in the roster.' % (first, last)
- makeSelection()
10. 此時(shí),可以為應(yīng)用程序入口點(diǎn)添加代碼了。此代碼會(huì)將應(yīng)用程序標(biāo)題輸出到“輸出”窗口中,然后調(diào)用 makeSelection() 函數(shù)。
- # main
- #
- # This is the application entry point. It simply prints the applicaion title
- # to the command line and then invokes the makeSelection() function.
- if __name__ == "__main__":
- print "Hockey Roster Application\n\n"
- makeSelection()
運(yùn)行應(yīng)用程序
現(xiàn)在可以測(cè)試應(yīng)用程序了。在 NetBeans IDE 中,Python 應(yīng)用程序的結(jié)果會(huì)輸出到“輸出”窗口中。
1. 在“項(xiàng)目”窗口中,右鍵單擊該項(xiàng)目節(jié)點(diǎn)并選擇“運(yùn)行”。
“輸出”窗口中將顯示該應(yīng)用程序,如下圖所示。
2. 要測(cè)試應(yīng)用程序,請(qǐng)鍵入 1,然后按 Enter 鍵。
系統(tǒng)將提示您輸入要添加的球員的名、姓及其球場(chǎng)位置,如下圖所示。
3. 嘗試添加更多球員。然后,在初始應(yīng)用程序提示符下鍵入 2 并按 Enter 鍵,以輸出球隊(duì)花名冊(cè)。
將在“輸出”窗口中輸出花名冊(cè),如下圖所示。
4. 現(xiàn)在,在初始應(yīng)用程序提示符下鍵入 3 并按 Enter 鍵,以搜索球員。
將在“輸出”窗口中再次顯示結(jié)果。
5. 現(xiàn)在,嘗試搜索已知的但未列在花名冊(cè)中的球員。
“輸出”窗口將告知您該球員未列在花名冊(cè)中。
小結(jié)
在本教程中,您使用 NetBeans IDE 創(chuàng)建了一個(gè)簡(jiǎn)單的用戶輸入 Python 應(yīng)用程序。您創(chuàng)建了一個(gè) Python 項(xiàng)目,在該項(xiàng)目中添加了一個(gè)空模塊,試用了代碼完成功能,并且運(yùn)行了應(yīng)用程序,同時(shí)在 IDE 的“輸出”窗口中查看了結(jié)果。
【編輯推薦】
聯(lián)系客服