使用NetBeans IDE開發Python應用程序詳解
本教程的目的是向您介紹在 NetBeans IDE 中使用 Python 編程語言的基礎知識。這樣,程序員就可以繼續深入了解該語言的功能。NetBeans 添加了對多種 JVM 語言和非 JVM 語言的支持,從而為程序員開辟了新的編程途徑,同時也為他們以多種不同的語言(包括 Python)進行開發提供了易于使用的環境。在本教程中,您將從頭開發一個小型應用程序 (hockeyRoster.py)。開發出的應用程序將用于記錄和管理球隊信息。
預計時間:20 分鐘
目錄
應用程序概述
設置
添加新 Python 安裝
安裝并配置 Python 安裝
創建項目
添加代碼
運行應用程序
小結
要學習本教程,您需要具備以下軟件和資源。
軟件或資源 | 要求的版本 |
NetBeans IDE | 6.5 或 6.7 Python EA 版本 |
Java Developer Kit (JDK) | 版本 6 或版本 5 |
Python | 2.5.x |
樣例數據庫 | 無要求 |
應用程序概述
在本基礎教程中,您將開發一個管理曲棍球隊信息的命令行應用程序,通過該應用程序可以將包含球員及其***球場位置和當前狀態("BENCHED" 或 "ACTIVE")的列表添加到外部文件中。這樣,您便可以通過 Python 列出該文件的內容,并更新記錄。本教程涉及文件輸入/輸出內容,但并未深入介紹有關數據庫的詳細信息。
設置
需要下載 NetBeans IDE 的 Python EA2 版本才能學完本教程。
注意:如果使用的是 NetBeans IDE 6.8,可通過以下操作從 Beta 更新中心安裝 Python 插件:
1. 在 IDE 的主菜單中,轉至“工具”>“插件”。
2. 在“插件”對話框中,單擊“可用插件”標簽并在“類別”列中查找 Python。
3. 選中 Python 和 Jython 發行版本插件旁邊的“安裝”框,然后單擊“安裝”。
4. 在“NetBeans IDE 安裝程序”對話框中,確認將要安裝 Python 和 Jython 插件,單擊“下一步”,然后接受許可證,安裝開始。
您可以在 "Python Platform Manager"(Python 平臺管理器)中配置 IDE 以支持 Python。此窗口允許您添加或刪除可用于 IDE 的 Python 安裝。使用 "Python Platform Manager"(Python 平臺管理器),還可以設置和配置 Python 路徑及 Java 路徑。通過執行上述操作,可以將 Java jar 文件或 Python 模塊置于 Path 變量中,以便在每次啟動 IDE 時都可以使用這些文件或模塊。此外,您甚至還可以為所配置的每個安裝定義不同的 Java 路徑或 Python 路徑。
添加新 Python 安裝
1. 在主工具欄中,選擇“工具”> "Python Platforms Manager"(Python 平臺管理器)。
此時會打開 "Python Platform Manager"(Python 平臺管理器),如下圖所示。
2. 如果缺省情況下未顯示 Python 安裝,請單擊 "New"(新建),瀏覽至要使用的安裝,然后選擇“打開”。
IDE 會將該安裝添加到 "Platforms"(平臺)列表中。如果您不確定安裝的位置,請單擊 "Auto Detect"(自動檢測)。
安裝并配置 Python 安裝
1. 選擇“工具”> "Python Installations"(Python 安裝)以打開 "Python Platform Manager"(Python 平臺管理器)。
2. 選擇要用于 IDE 的 Python 安裝,然后單擊 "Make Default"(設為缺省)。
3. 在 "Command Arguments"(命令參數)文本字段中,輸入安裝的任何命令參數。如有必要,也可以對平臺重命名。
4. 單擊 "Close"(關閉)關閉 "Python Platform Manager"(Python 平臺管理器)。
創建項目
首先,在 IDE 中創建一個新 Python 項目。“新建項目”向導包含許多項目類型的內置模板。訪問“新建項目”向導有多種方法。可以在主工具欄中選擇“文件”>“新建項目”,也可以在“項目”窗口中單擊鼠標右鍵,然后選擇“新建項目”。
1. 在主工具欄中,選擇“文件”>“新建項目”。
此時將打開“新建項目”向導,其中顯示了 "Python" 類別。
注意:當計算機中只是安裝了 IDE 的 Python EA 版本時,"Python" 會顯示為項目類別。如果 Python EA 是作為插件添加到 IDE 中的,則可能會顯示其他類別。
2. 選擇 "Python Project"(Python 項目)作為項目類型,如下圖所示。單擊“下一步”。
3. 輸入 HockeyRoster 作為項目名稱,如下圖所示。選擇要用于該項目的 Python 版本,然后重命名主文件 HockeyRoster.py。
4. 將 "Set as Main Project"(設置為主項目)復選框和 "Create Main File"(創建主文件)復選框保留為選中狀態。單擊“完成”。
IDE 將基于項目的名稱來創建項目文件夾。您可以更改此文件夾的名稱和位置。
5. 從 "Python Platform"(Python 平臺)下拉列表中選擇要使用的 Python 平臺,然后單擊“完成”以創建項目。
IDE 將創建該項目。請注意在“項目”窗口中是如何顯示該項目的。
請注意,在 IDE 的源代碼編輯器中將打開顯示一些基本信息的 HockeyRoster.py 文件,如下圖所示。NetBeans IDE 會自動記錄項目的作者和創建日期,并且還會提供一個輸出 "Hello" 的簡短樣例程序。
6.. 現在,您要為該應用程序創建第二個源文件。在“項目”窗口中,右鍵單擊該項目的 "Sources"(源)節點,然后選擇“新建”> "Empty Module"(空模塊),如下圖所示。
此時會打開 "New Empty Module"(新建空模塊)向導。
7. 鍵入 Player 作為文件名,然后單擊“完成”。
將在源代碼編輯器中打開該文件。此時,“項目”窗口應與下圖類似。
#p#
添加代碼
在此部分,您要將 Python 代碼添加到在本教程上一部分所創建的文件中。
1. 如果尚未打開 Player.py 文件,請在源代碼編輯器中將其打開。
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
此時,源代碼編輯器應與下圖類似。請注意 IDE 是如何幫助您輸入括號和冒號的。
3. 在源代碼編輯器中打開 HockeyPlayer.py。
4. 刪除 Hockeyplayer.py 中的現有代碼,然后鍵入以下代碼。這是添加的***段代碼,它主要由注釋組成,但還將執行以下兩項重要操作:
◆ 從先前創建的 Player 模塊中導入 Player 類。
◆ 定義一個列表,以記錄每個 Player 對象。
- # 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. 現在,您要向應用程序中添加更多代碼。此段代碼將創建一個用于在“輸出”窗口中生成輸出的函數(該輸出基于用戶所輸入的內容),從而為應用程序創建一個選擇器。
- # 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. 右鍵單擊源代碼編輯器中的任意位置,然后選擇“格式化代碼”,如下圖所示。
現在,您要添加一些代碼,以接受來自“輸出”窗口的鍵盤輸入。用戶可以輸入球員的名、姓及場上位置。
- # 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()
請注意 IDE 的代碼完成功能是如何在您鍵入代碼時提供建議的,如下圖所示。
8. 在此步中,您要添加將列表內容以報告形式輸出到“輸出”窗口的代碼。
- # 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. 現在,您要輸入一些代碼,以提取從“輸出”窗口輸入的球員姓名,并在花名冊中搜索與其匹配的內容。
- # 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. 此時,可以為應用程序入口點添加代碼了。此代碼會將應用程序標題輸出到“輸出”窗口中,然后調用 makeSelection() 函數。
- # 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()
運行應用程序
現在可以測試應用程序了。在 NetBeans IDE 中,Python 應用程序的結果會輸出到“輸出”窗口中。
1. 在“項目”窗口中,右鍵單擊該項目節點并選擇“運行”。
“輸出”窗口中將顯示該應用程序,如下圖所示。
2. 要測試應用程序,請鍵入 1,然后按 Enter 鍵。
系統將提示您輸入要添加的球員的名、姓及其球場位置,如下圖所示。
3. 嘗試添加更多球員。然后,在初始應用程序提示符下鍵入 2 并按 Enter 鍵,以輸出球隊花名冊。
將在“輸出”窗口中輸出花名冊,如下圖所示。
4. 現在,在初始應用程序提示符下鍵入 3 并按 Enter 鍵,以搜索球員。
將在“輸出”窗口中再次顯示結果。
5. 現在,嘗試搜索已知的但未列在花名冊中的球員。
“輸出”窗口將告知您該球員未列在花名冊中。
小結
在本教程中,您使用 NetBeans IDE 創建了一個簡單的用戶輸入 Python 應用程序。您創建了一個 Python 項目,在該項目中添加了一個空模塊,試用了代碼完成功能,并且運行了應用程序,同時在 IDE 的“輸出”窗口中查看了結果。