使用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

2012-07-18 11:29:32

ibmdw

2011-07-26 16:33:56

iPhone Delegate

2009-06-15 14:28:00

NetBeansJavafx

2019-12-16 10:01:54

Java开发Web

2011-03-21 09:05:40

IronRubyWindows Pho

2010-08-04 10:13:40

FlexBuilder

2009-06-10 14:53:25

netbeans st实例

2011-07-18 10:21:04

iOS Visual Stu iphone

2013-02-21 14:15:41

开发Tizen

2013-02-21 14:14:40

开发Tizen

2012-02-08 15:06:31

ibmdw

2022-09-19 00:37:13

SaaS云计算开发

2009-07-23 13:26:21

2011-08-10 11:25:59

ipad信息架构数据结构

2010-01-04 10:41:14

Silverlight
点赞
收藏

51CTO技术栈公众号