日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用NetBeans IDE開發(fā)Python應(yīng)用程序詳解

本教程的目的是向您介紹在 NetBeans IDE 中使用 Python 編程語言的基礎(chǔ)知識。這樣,程序員就可以繼續(xù)深入了解該語言的功能。NetBeans 添加了對多種 JVM 語言和非 JVM 語言的支持,從而為程序員開辟了新的編程途徑,同時也為他們以多種不同的語言(包括 Python)進(jìn)行開發(fā)提供了易于使用的環(huán)境。在本教程中,您將從頭開發(fā)一個小型應(yīng)用程序 (hockeyRoster.py)。開發(fā)出的應(yīng)用程序?qū)⒂糜谟涗浐凸芾砬蜿犘畔ⅰ?/p>

成都創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)貴南,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

預(yù)計時間:20 分鐘

目錄

應(yīng)用程序概述

設(shè)置

添加新 Python 安裝

安裝并配置 Python 安裝

創(chuàng)建項目

添加代碼

運行應(yīng)用程序

小結(jié)

要學(xué)習(xí)本教程,您需要具備以下軟件和資源。

軟件或資源要求的版本
NetBeans IDE6.5 或 6.7 Python EA 版本
Java Developer Kit (JDK)版本 6 或版本 5
Python2.5.x
樣例數(shù)據(jù)庫無要求

應(yīng)用程序概述

在本基礎(chǔ)教程中,您將開發(fā)一個管理曲棍球隊信息的命令行應(yīng)用程序,通過該應(yīng)用程序可以將包含球員及其***球場位置和當(dāng)前狀態(tài)("BENCHED" 或 "ACTIVE")的列表添加到外部文件中。這樣,您便可以通過 Python 列出該文件的內(nèi)容,并更新記錄。本教程涉及文件輸入/輸出內(nèi)容,但并未深入介紹有關(guān)數(shù)據(jù)庫的詳細(xì)信息。

設(shè)置

需要下載 NetBeans IDE 的 Python EA2 版本才能學(xué)完本教程。

注意:如果使用的是 NetBeans IDE 6.8,可通過以下操作從 Beta 更新中心安裝 Python 插件:

1. 在 IDE 的主菜單中,轉(zhuǎn)至“工具”>“插件”。

2. 在“插件”對話框中,單擊“可用插件”標(biāo)簽并在“類別”列中查找 Python。

3. 選中 Python 和 Jython 發(fā)行版本插件旁邊的“安裝”框,然后單擊“安裝”。

4. 在“NetBeans IDE 安裝程序”對話框中,確認(rèn)將要安裝 Python 和 Jython 插件,單擊“下一步”,然后接受許可證,安裝開始。

您可以在 "Python Platform Manager"(Python 平臺管理器)中配置 IDE 以支持 Python。此窗口允許您添加或刪除可用于 IDE 的 Python 安裝。使用 "Python Platform Manager"(Python 平臺管理器),還可以設(shè)置和配置 Python 路徑及 Java 路徑。通過執(zhí)行上述操作,可以將 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"(設(shè)為缺省)。

3. 在 "Command Arguments"(命令參數(shù))文本字段中,輸入安裝的任何命令參數(shù)。如有必要,也可以對平臺重命名。

4. 單擊 "Close"(關(guān)閉)關(guān)閉 "Python Platform Manager"(Python 平臺管理器)。

創(chuàng)建項目

首先,在 IDE 中創(chuàng)建一個新 Python 項目?!靶陆椖俊毕?qū)ОS多項目類型的內(nèi)置模板。訪問“新建項目”向?qū)в卸喾N方法??梢栽谥鞴ぞ邫谥羞x擇“文件”>“新建項目”,也可以在“項目”窗口中單擊鼠標(biāo)右鍵,然后選擇“新建項目”。

1. 在主工具欄中,選擇“文件”>“新建項目”。

此時將打開“新建項目”向?qū)?,其中顯示了 "Python" 類別。

注意:當(dāng)計算機(jī)中只是安裝了 IDE 的 Python EA 版本時,"Python" 會顯示為項目類別。如果 Python EA 是作為插件添加到 IDE 中的,則可能會顯示其他類別。

2. 選擇 "Python Project"(Python 項目)作為項目類型,如下圖所示。單擊“下一步”。

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

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

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

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

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

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

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

此時會打開 "New Empty Module"(新建空模塊)向?qū)А?/p>

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

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

#p#

添加代碼

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

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

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

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

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

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

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

◆ 從先前創(chuàng)建的 Player 模塊中導(dǎo)入 Player 類。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

運行應(yīng)用程序

現(xiàn)在可以測試應(yīng)用程序了。在 NetBeans IDE 中,Python 應(yīng)用程序的結(jié)果會輸出到“輸出”窗口中。

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

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

2. 要測試應(yīng)用程序,請鍵入 1,然后按 Enter 鍵。

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

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

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

4. 現(xiàn)在,在初始應(yīng)用程序提示符下鍵入 3 并按 Enter 鍵,以搜索球員。

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

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

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

小結(jié)

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


名稱欄目:使用NetBeans IDE開發(fā)Python應(yīng)用程序詳解
地址分享:http://m.5511xx.com/article/cosejpc.html