成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用NetBeans IDE開發Python應用程序詳解

開發 后端 前端
本教程的目的是向您介紹在 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 平臺管理器),如下圖所示。

使用 NetBeans IDE 開發 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 項目)作為項目類型,如下圖所示。單擊“下一步”。

使用 NetBeans IDE 開發 Python 應用程序

3. 輸入 HockeyRoster 作為項目名稱,如下圖所示。選擇要用于該項目的 Python 版本,然后重命名主文件 HockeyRoster.py。

使用 NetBeans IDE 開發 Python 應用程序

4. 將 "Set as Main Project"(設置為主項目)復選框和 "Create Main File"(創建主文件)復選框保留為選中狀態。單擊“完成”。

IDE 將基于項目的名稱來創建項目文件夾。您可以更改此文件夾的名稱和位置。

5. 從 "Python Platform"(Python 平臺)下拉列表中選擇要使用的 Python 平臺,然后單擊“完成”以創建項目。

IDE 將創建該項目。請注意在“項目”窗口中是如何顯示該項目的。

使用 NetBeans IDE 開發 Python 應用程序

請注意,在 IDE 的源代碼編輯器中將打開顯示一些基本信息的 HockeyRoster.py 文件,如下圖所示。NetBeans IDE 會自動記錄項目的作者和創建日期,并且還會提供一個輸出 "Hello" 的簡短樣例程序。

使用 NetBeans IDE 開發 Python 應用程序

6.. 現在,您要為該應用程序創建第二個源文件。在“項目”窗口中,右鍵單擊該項目的 "Sources"(源)節點,然后選擇“新建”> "Empty Module"(空模塊),如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

此時會打開 "New Empty Module"(新建空模塊)向導。

7. 鍵入 Player 作為文件名,然后單擊“完成”。

使用 NetBeans IDE 開發 Python 應用程序

將在源代碼編輯器中打開該文件。此時,“項目”窗口應與下圖類似。

使用 NetBeans IDE 開發 Python 應用程序

#p#

添加代碼

在此部分,您要將 Python 代碼添加到在本教程上一部分所創建的文件中。

1. 如果尚未打開 Player.py 文件,請在源代碼編輯器中將其打開。

2. 在 Player.py 文件中鍵入以下代碼。

  1. # Player.py 
  2. # 
  3. # Container to hold our player objects 
  4.  
  5.  
  6. class Player: 
  7.  
  8.  
  9.     # Player attributes 
  10.  
  11.      
  12.     id = 0 
  13.     first = None 
  14.     last = None 
  15.     position = None 
  16.  
  17.     
  18.     # Function used to create a player object 
  19.  
  20.  
  21.     def create(self, id, first, last, position): 
  22.         self.id = id 
  23.         self.first = first 
  24.         self.last = last 
  25.         self.position = position 

此時,源代碼編輯器應與下圖類似。請注意 IDE 是如何幫助您輸入括號和冒號的。

使用 NetBeans IDE 開發 Python 應用程序

3. 在源代碼編輯器中打開 HockeyPlayer.py。

4. 刪除 Hockeyplayer.py 中的現有代碼,然后鍵入以下代碼。這是添加的***段代碼,它主要由注釋組成,但還將執行以下兩項重要操作:

◆ 從先前創建的 Player 模塊中導入 Player 類。

◆ 定義一個列表,以記錄每個 Player 對象。

  1. # HockeyRoster.py 
  2. # 
  3. # Implementation logic for the HockeyRoster application 
  4.  
  5. # Import Player class from the Player module 
  6. from Player import Player 
  7.  
  8. # Define a list to hold each of the Player objects 
  9. playerList = [] 

5. 現在,您要向應用程序中添加更多代碼。此段代碼將創建一個用于在“輸出”窗口中生成輸出的函數(該輸出基于用戶所輸入的內容),從而為應用程序創建一個選擇器。

  1. # makeSelection() 
  2. # 
  3. # Creates a selector for our application.  The function prints output to the  
  4. # command line.  It then takes a parameter as keyboard input at the command line 
  5. # in order to choose our application option. 
  6.  
  7.  
  8. def makeSelection(): 
  9.     validOptions = ['1','2','3','4'
  10.     print "Please choose an option\n" 
  11.  
  12.  
  13.     selection = raw_input("Press 1 to add a player, 2 to print the team roster, 
  14. 3 to search for a player on the team, 4 to quit: "
  15.     if selection not in validOptions: 
  16.         print "Not a valid option, please try again\n" 
  17.         makeSelection() 
  18.     else
  19.         if selection == '1'
  20.             addPlayer() 
  21.         elif selection == '2'
  22.             printRoster() 
  23.         elif selection == '3'
  24.             searchRoster() 
  25.         else
  26.             print "Thanks for using the HockeyRoster application." 

6. 右鍵單擊源代碼編輯器中的任意位置,然后選擇“格式化代碼”,如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

現在,您要添加一些代碼,以接受來自“輸出”窗口的鍵盤輸入。用戶可以輸入球員的名、姓及場上位置。

  1. # addPlayer() 
  2. # 
  3. # Accepts keyboard input to add a player object to the roster list.  This function 
  4. # creates a new player object each time it is invoked and appends it to the list. 
  5. def addPlayer(): 
  6.     addNew = 'Y' 
  7.     print "Add a player to the roster by providing the following information\n" 
  8.     while addNew.upper() == 'Y'
  9.         first = raw_input("First Name: "
  10.         last = raw_input("Last Name: "
  11.         position = raw_input("Position: "
  12.         id = len(playerList) 
  13.         player = Player() 
  14.         player.create(id, first, last, position) 
  15.         playerList.append(player) 
  16.         print "Player successfully added to the team roster\n" 
  17.         addNew = raw_input("Add another? (Y or N)"
  18.     makeSelection() 

請注意 IDE 的代碼完成功能是如何在您鍵入代碼時提供建議的,如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

8. 在此步中,您要添加將列表內容以報告形式輸出到“輸出”窗口的代碼。

  1. # printRoster() 
  2. # 
  3. # Prints the contents of the list to the command line as a report 
  4. def printRoster(): 
  5.     print "====================\n" 
  6.     print "Complete Team Roster\n" 
  7.     print "======================\n\n" 
  8.     for player in playerList: 
  9.         print "%s %s - %s" % (player.first, player.last, player.position) 
  10.     print "\n" 
  11.     print "=== End of Roster ===\n" 
  12.     makeSelection() 

9. 現在,您要輸入一些代碼,以提取從“輸出”窗口輸入的球員姓名,并在花名冊中搜索與其匹配的內容。

  1. # searchRoster() 
  2. # 
  3. # Takes input from the command line for a player's name to search within the 
  4. # roster list.  If the player is found in the list then an affirmative message 
  5. # is printed.  If not found, then a negative message is printed. 
  6. def searchRoster(): 
  7.     index = 0 
  8.     found = False 
  9.     print "Enter a player name below to search the team\n" 
  10.     first = raw_input("First Name: "
  11.     last = raw_input("Last Name: "
  12.     position = None 
  13.     while index < len(playerList): 
  14.         player = playerList[index] 
  15.         if player.first.upper() == first.upper() or player.last.upper() == last.upper(): 
  16.             found = True 
  17.             position = player.position 
  18.         index = index + 1 
  19.     if found: 
  20.         print '%s %s is in the roster as %s' % (first, last, position) 
  21.     else
  22.         print '%s %s is not in the roster.' % (first, last) 
  23.     makeSelection() 

10. 此時,可以為應用程序入口點添加代碼了。此代碼會將應用程序標題輸出到“輸出”窗口中,然后調用 makeSelection() 函數。

  1. # main 
  2. # 
  3. # This is the application entry point.  It simply prints the applicaion title 
  4. # to the command line and then invokes the makeSelection() function. 
     
  5. if __name__ == "__main__"
  6.     print "Hockey Roster Application\n\n" 
  7.     makeSelection() 

運行應用程序

現在可以測試應用程序了。在 NetBeans IDE 中,Python 應用程序的結果會輸出到“輸出”窗口中。

1. 在“項目”窗口中,右鍵單擊該項目節點并選擇“運行”。

“輸出”窗口中將顯示該應用程序,如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

2. 要測試應用程序,請鍵入 1,然后按 Enter 鍵。

系統將提示您輸入要添加的球員的名、姓及其球場位置,如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

3. 嘗試添加更多球員。然后,在初始應用程序提示符下鍵入 2 并按 Enter 鍵,以輸出球隊花名冊。

將在“輸出”窗口中輸出花名冊,如下圖所示。

使用 NetBeans IDE 開發 Python 應用程序

4. 現在,在初始應用程序提示符下鍵入 3 并按 Enter 鍵,以搜索球員。

將在“輸出”窗口中再次顯示結果。

使用 NetBeans IDE 開發 Python 應用程序

5. 現在,嘗試搜索已知的但未列在花名冊中的球員。

“輸出”窗口將告知您該球員未列在花名冊中。

使用 NetBeans IDE 開發 Python 應用程序

小結

在本教程中,您使用 NetBeans IDE 創建了一個簡單的用戶輸入 Python 應用程序。您創建了一個 Python 項目,在該項目中添加了一個空模塊,試用了代碼完成功能,并且運行了應用程序,同時在 IDE 的“輸出”窗口中查看了結果。

 

責任編輯:陳貽新 來源: NetBeans
相關推薦

2009-05-27 14:31:53

NetBeansEclipsPHP

2009-06-10 14:59:04

Netbeans 6.應用程序

2010-03-11 15:23:35

Pythonfor s

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-02-22 10:23:43

2011-07-26 16:33:56

iPhone Delegate

2012-07-18 11:29:32

ibmdw

2009-06-10 14:53:25

netbeans st實例

2009-06-15 14:28:00

NetBeansJavafx

2019-12-16 10:01:54

Java開發Web

2010-08-04 10:13:40

FlexBuilder

2011-03-21 09:05:40

IronRubyWindows Pho

2013-02-21 14:14:40

開發Tizen

2013-02-21 14:15:41

開發Tizen

2010-01-04 10:41:14

Silverlight

2011-08-10 11:25:59

ipad信息架構數據結構

2009-06-10 11:47:32

Android應用程序模塊

2009-07-23 13:26:21

2012-02-08 15:06:31

ibmdw

2022-09-19 00:37:13

SaaS云計算開發
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品成人一区二区 | 久久久久久高潮国产精品视 | 久久久69| 日韩视频在线免费观看 | 日韩av网址在线观看 | 99riav国产一区二区三区 | 国产亚洲欧美另类一区二区三区 | 久久激情视频 | www.国产日本| 日本手机看片 | 久久久久久国产精品免费免费狐狸 | 欧美2区 | 欧美中文字幕一区二区三区亚洲 | 黄篇网址| 日操夜操 | 午夜免费在线电影 | 欧美自拍第一页 | 精品国产乱码久久久久久蜜退臀 | 午夜精品久久久久99蜜 | 欧美激情精品久久久久久变态 | 亚洲国产视频一区二区 | 91人人爽 | 亚洲精品毛片av | 三级视频在线观看电影 | 欧美久久一区二区三区 | 久艹网站 | 激情一区二区三区 | 91电影| 色综合国产 | 台湾佬成人网 | 日本欧美在线 | 在线激情视频 | 国产精品国产a | 日韩视频精品 | 欧美日本一区 | 天天射影院| 国产精彩视频在线观看 | 亚洲一区 中文字幕 | 国产免费一区二区 | 欧美精品网| 精品一区二区久久久久久久网站 |