Python 简明教程

Python - GUIs

在本章中,您将了解一些流行的 Python IDE (Integrated Development Environment) 以及如何使用 IDE 进行程序开发。

In this chapter, you will learn about some popular Python IDEs (Integrated Development Environment), and how to use IDE for program development.

要使用 Python 的脚本模式,您需要将一系列 Python 指令保存在一个文本文件中,并以 .py 扩展名保存。您可以使用操作系统上可用的任何文本编辑器。每当解释器遇到错误时,源代码都需要被编辑并一再运行。为了避免这种繁琐的方法,使用了 IDE。IDE 是一个一站式解决方案,用于键入、编辑源代码、检测错误和执行程序。

To use the scripted mode of Python, you need to save the sequence of Python instructions in a text file and save it with .py extension. You can use any text editor available on the operating system. Whenever the interpreter encounters errors, the source code needs to be edited and run again and again. To avoid this tedious method, IDE is used. An IDE is a one stop solution for typing, editing the source code, detecting the errors and executing the program.

IDLE

Python 的标准库包含 IDLE 模块。IDLE 是 Integrated Development and Learning Environment 的缩写。顾名思义,当一个人处于学习阶段时,它非常有用。它包括一个 Python 交互式 Shell 和一个代码编辑器,针对 Python 语言结构进行了定制。它的一些重要特性包括语法突出显示、自动完成、可自定义的界面等。

Python’s standard library contains the IDLE module. IDLE stands for Integrated Development and Learning Environment. As the name suggests, it is useful when one is in the learning stage. It includes a Python interactive shell and a code editor, customized to the needs of Python language structure. Some of its important features include syntax highlighting, auto-completion, customizable interface etc.

要编写一个 Python 脚本,从“文件”菜单中打开一个新的文本编辑器窗口。

To write a Python script, open a new text editor window from the File menu.

idle module

一个新的编辑器窗口打开,您可以在其中输入 Python 代码。使用“运行”菜单保存并运行它。

A new editor window opens in which you can enter the Python code. Save it and run it with Run menu.

new window

Jupyter Notebook

最初作为 IPython 的 Web 界面开发,Jupyter Notebook 支持多种语言。名称本身取自所支持语言的名称中的字母 − Ju*lia, *PYT*hon and *R 。Jupyter Notebook 是一个客户端服务器应用程序。服务器在本地主机上启动,浏览器充当其客户端。

Initially developed as a web interface for IPython, Jupyter Notebook supports multiple languages. The name itself derives from the alphabets from the names of the supported languages − Ju*lia, *PYT*hon and *R. Jupyter notebook is a client server application. The server is launched at the localhost, and the browser acts as its client.

使用 PIP 安装 Jupyter Notebook −

Install Jupyter notebook with PIP −

pip3 install jupyter

从命令行调用。

Invoke from the command line.

C:\Users\Acer>jupyter notebook

服务器在本地主机的 8888 端口号上启动。

The server is launched at localhost’s 8888 port number.

server launched

您的系统的默认浏览器将打开一个链接 http://localhost:8888/tree 以显示仪表盘。

The default browser of your system opens a link http://localhost:8888/tree to display the dashboard.

jupyter

打开一个新的 Python 笔记本。它显示 IPython 样式的输入单元格。输入 Python 指令并运行单元格。

Open a new Python notebook. It shows IPython style input cell. Enter Python instructions and run the cell.

python notebook

Jupyter Notebook 是一个多功能工具,被数据科学家广泛用于显示内联数据可视化。该笔记本可以方便地转换为 PDF、HTML 或降价格式并分发。

Jupyter notebook is a versatile tool, used very extensively by data scientists to display inline data visualizations. The notebook can be conveniently converted and distributed in PDF, HTML or Markdown format.

VS Code

Microsoft 开发了一个名为 VS Code (Visual Studio Code) 的源代码编辑器,它支持多种语言,包括 C++、Java、Python 等。它提供了语法突出显示、自动完成、调试器和版本控制等特性。

Microsoft has developed a source code editor called VS Code (Visual Studio Code) that supports multiple languages including C++, Java, Python and others. It provides features such as syntax highlighting, autocomplete, debugger and version control.

VS Code 是一款免费软件。可以从 https://code.visualstudio.com/ 下载并安装。

VS Code is a freeware. It is available for download and install from https://code.visualstudio.com/.

从开始菜单(在 Windows 中)启动 VS Code。

Launch VS Code from the start menu (in Windows).

vs code window

您还可以从命令行启动 VS Code −

You can also launch VS Code from command line −

C:\test>code .

如果不安装相应的语言扩展,则无法使用 VS Code。VS Code Extensions 市场有许多用于语言编译器和其他实用程序的扩展。从“扩展”选项卡(Ctrl+Shift+X)中搜索 Python 扩展并安装它。

VS Code cannot be used unless respective language extension is not installed. VS Code Extensions marketplace has a number of extensions for language compilers and other utilities. Search for Python extension from the Extension tab (Ctrl+Shift+X) and install it.

vs code extensions

激活 Python 扩展后,您需要设置 Python 解释器。按 Ctrl+Shift+P 并选择 Python 解释器。

After activating Python extension, you need to set the Python interpreter. Press Ctrl+Shift+P and select Python interpreter.

select interpreter

打开一个新的文本文件,输入 Python 代码并保存该文件。

Open a new text file, enter Python code and save the file.

python code file

打开命令提示符终端并运行该程序。

Open a command prompt terminal and run the program.

command prompt terminal

PyCharm

PyCharm 是另一个流行的 Python IDE。它是由捷克软件公司 JetBrains 开发的。它的功能包括代码分析、图形调试器、与版本控制系统集成等。PyCharm 支持使用 Django 进行 Web 开发。

PyCharm is another popular Python IDE. It has been developed by JetBrains, a Czech software company. Its features include code analysis, a graphical debugger, integration with version control systems etc. PyCharm supports web development with Django.

社区版本和专业版本均可从 https://www.jetbrains.com/pycharm/download 下载。

The community as well as professional editions can be downloaded from https://www.jetbrains.com/pycharm/download.

下载并安装最新版本:2022.3.2 并打开 PyCharm。欢迎界面如下所示 −

Download, install the latest Version: 2022.3.2 and open PyCharm. The Welcome screen appears as below −

welcome to pycharm

新项目启动时,PyCharm 会根据所选文件夹位置和 Python 解释器版本为其创建虚拟环境。

When you start a new project, PyCharm creates a virtual environment for it based on the choice of folder location and the version of Python interpreter chosen.

new project

您现在可以添加该项目所需的 Python 脚本。此处我们向 main.py 文件中添加示例 Python 代码。

You can now add one or more Python scripts required for the project. Here we add a sample Python code in main.py file.

python project

选择运行菜单中的选项或使用 Shift+F10 快捷键执行程序。

To execute the program, choose from Run menu or use Shift+F10 shortcut.

run the program

输出将显示在控制台窗口中,如下所示 −

Output will be displayed in the console window as shown below −

output displayed