In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use NetBeans IDE to develop Python applications. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Introduces you to the basics of using the Python programming language in NetBeans IDE. In this way, programmers can continue to learn more about the functions of the language. NetBeans adds support for a variety of JVM and non-JVM languages, thus opening up new programming approaches for programmers and providing an easy-to-use environment for them to develop in many different languages, including Python. In this tutorial, you will develop a small application (hockeyRoster.py) from scratch. The developed application will be used to record and manage team information.
To follow this tutorial, you need the following software and resources.
Software or resource required version NetBeans IDE6.5 or 6.7 Python EA version Java Developer Kit (JDK) version 6 or version 5Python2.5.x sample database is not required
Application Overview
In this basic tutorial, you will develop a command-line application that manages hockey team information, through which you can add a list of players and their court location and current status ("BENCHED" or "ACTIVE") to an external file. This allows you to list the contents of the file through Python and update the record. This tutorial covers file input / output, but does not provide in-depth details about the database.
Set up
You need to download the Python EA2 version of NetBeans IDE to complete this tutorial.
Note: if you are using NetBeans IDE 6.8, you can install the Python plug-in from the Beta Update Center by:
1. In the main menu of IDE, go to tools > plug-ins.
two。 In the plug-ins dialog box, click the available plug-ins tab and look for Python in the Category column.
3. Check the install box next to the Python and Jython distribution plug-ins, and then click install.
4. In the NetBeans IDE installer dialog box, confirm that the Python and Jython plug-ins will be installed, click next, then accept the license, and the installation begins.
You can configure IDE in Python Platform Manager (Python platform Manager) to support Python. This window allows you to add or remove Python installations that are available for IDE. Using Python Platform Manager (Python platform Manager), you can also set and configure Python and Java paths. By doing this, you can place Java jar files or Python modules in the Path variable so that they can be used every time you start IDE. In addition, you can even define a different Java or Python path for each installation that you configure.
Add a new Python installation
1. In the main toolbar, select tools > Python Platforms Manager (Python platform Manager).
The Python Platform Manager (Python platform Manager) opens, as shown in the following figure.
two。 If the Python installation is not displayed by default, click New, browse to the installation you want to use, and select Open.
IDE will add the installation to the "Platforms" list. If you are not sure where to install, click Auto Detect (automatic detection).
Install and configure Python installation
1. Select tools > Python Installations (Python installation) to open Python Platform Manager (Python platform Manager).
two。 Select the Python installation you want to use for IDE, and then click "Make Default" (set to default).
3. In the Command Arguments (command parameters) text field, enter any command parameters that are installed. You can also rename the platform if necessary.
4. Click "Close" to close "Python Platform Manager" (Python platform Manager).
Create a project
First, create a new Python project in IDE. The New Project wizard contains built-in templates for many project types. There are several ways to access the New Project wizard. You can select File > New Project in the main toolbar, or you can right-click in the Project window and select New Project.
1. In the main toolbar, select File > New Project.
The New Project wizard opens, showing the Python category.
Note: when only the Python EA version of IDE is installed on your computer, "Python" appears as a project category. If Python EA is added to IDE as a plug-in, other categories may be displayed.
two。 Select "Python Project" (Python project) as the project type, as shown in the following figure. Click next.
3. Enter HockeyRoster as the project name, as shown in the following figure. Select the version of Python to use for the project, and then rename the master file HockeyRoster.py.
4. Leave the Set as Main Project (set as master project) check box and the Create Main File (create master file) check box selected. Click finish.
IDE creates a project folder based on the name of the project. You can change the name and location of this folder.
5. Select the Python platform you want to use from the Python Platform (Python platform) drop-down list, and then click finish to create the project.
IDE will create the project. Notice how the project is displayed in the Project window.
Notice that the HockeyRoster.py file showing some basic information opens in IDE's source code editor, as shown in the following figure. NetBeans IDE automatically records the author and creation date of the project, and provides a short sample program that outputs "Hello".
6.。 Now you are going to create a second source file for the application. In the Project window, right-click the Sources (source) node of the project, and select New > Empty Module (empty module), as shown in the following figure.
The New Empty Module wizard opens.
7. Type Player as the file name, and then click finish.
The file will be opened in the source code editor. At this point, the Project window should look similar to the following figure.
Add code
In this section, you will add Python code to the file you created in the previous part of this tutorial.
1. If you have not already opened the Player.py file, open it in the source code editor.
two。 Type the following code in the Player.py file.
# 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
At this point, the source code editor should look similar to the following figure. Notice how IDE helps you enter parentheses and colons.
3. Open HockeyPlayer.py in the source code editor.
4. Delete the existing code in Hockeyplayer.py, and then type the following code. This is the added * * code, which consists mainly of comments, but will also perform the following two important actions:
◆ imports the Player class from the Player module you created earlier.
◆ defines a list to record each Player object.
# 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. Now you want to add more code to your application. This code creates a function that generates output in the output window (based on what the user enters), creating a selector for the application.
# 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 = ['1th to add a player, 2 to print the team roster, 3 to search for a player on the team, 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 = = '1percent: addPlayer () elif selection = =' 2percent: printRoster () elif selection = = '3percent: searchRoster () else: print" Thanks for using the HockeyRoster application. "
6. Right-click anywhere in the source code editor and select format Code, as shown in the following figure.
Now you want to add some code to accept keyboard input from the output window. The user can enter the first name, last name and position of the player.
# 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 () = 'Yee: 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 ()
Notice how the code completion feature of IDE provides advice as you type the code, as shown in the following figure.
8. In this step, you add code that outputs the contents of the list as a report to the output window.
# 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"% (player.first, player.last, player.position) print "\ n" print "= End of Roster = =\ n" makeSelection ()
9. Now you need to enter some code to extract the player name entered from the output window and search the roster for matches.
# 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. 现在,尝试搜索已知的但未列在花名册中的球员。 "输出"窗口将告知您该球员未列在花名册中。In this tutorial, you created a simple user input Python application using NetBeans IDE. You created a Python project, added an empty module to the project, tried code completion, ran the application, and viewed the results in the output window of IDE.
On how to use NetBeans IDE to develop Python applications to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.