0. 前言
最近在查阅Tkinter相关资料,由于在安装后的Tkinter包路径(Python安装路径下的lib\tkinter目录)并没有找到相关的官方文档(诸如pdf、html、chm、txt等格式的文件),所以就只能借助help()函数来查看其官方帮助信息了。
通过摸索,发现Python内置的help()函数能提供不少有用的信息,我在此做了一些归纳总结,特此分享给需要的朋友们。
本文分享内容的目录如下:
前言
help()函数简介
help()语法说明
help()使用实例
结束语
注:本机使用的是Python自带的 IDLE (Python 3.8 64-bit)
1. help()函数简介
help()是Python的内置函数之一。通过help()可以调用Python内置的帮助系统,help()函数主要在交互式中使用。
我们在编写Python语言代码使用包、模块、类、函数或方法时,有时会记不清其用途,此时就可以通过help()函数来快捷地查看相关帮助信息。
help()函数特性:
1) 使用方便(是Python内置函数,无需import,直接调用help()即可)
2) 能查看多种类型的帮助信息(包括:包Package、模块Module、类Class、函数Function、方法Method等)
3) 可以将查看的帮助信息保存到指定路径文件中
注意help()函数与dir()函数的区别:
1) help()函数用于查看给定对象的用途的详细说明。
2) dir()函数用于查看给定对象的属性、方法列表。
2. help()语法说明
一、语法:
help([object])
二、参数及返回:
注:如果给定的参数列表中出现斜杠/,则意味着斜杠/之前的参数仅是定位的。
3. help()使用实例
3.1 查看包的帮助信息
如,通过执行help('tkinter')查看tkinter包的帮助信息:
上图中可以看到在控制台上输出了提示信息(Squeezed text (21469 lines).)。原因是由于查询到的tkinter包的帮助信息比较多,所以没有直接在控制台上打印输出。鼠标移动到该提示文字区域时,会弹出浮动提示信息Double-click to expand, right-click for more options.,提示:
一、双击该文字区域可以在控制台展开显示该帮助信息
>>> help('tkinter')
Help on package tkinter:
NAME
tkinter - Wrapper functions for Tcl/Tk.
MODULE REFERENCE
https://docs.python.org/3.8/library/tkinter
..........
..........
..........
FILE
c:\develop\python\lib\tkinter\__init__.p
二、或者右击显示菜单选项(如下图示)
1)选择copy菜单项:复制该帮助信息到剪贴板,可以复制到一个文件中进行保存。
2)选择view菜单项:直接打开一个新窗口显示该帮助信息(如下),可以在该新窗口中方便地进行查看。
3.2 查看模块的帮助信息
如,通过执行help('tkinter.ttk')查看tkinter包下的ttk模块的帮助信息:
3.3 查看某个包/模块下的类的帮助信息
如,通过执行help('tkinter.XView')查看tkinter包下XView类的帮助信息:
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import tkinter
>>> help('tkinter.XView')
Help on class XView in tkinter:
tkinter.XView = class XView(builtins.object)
| Mix-in class for querying and changing the horizontal position
| of a widget's window.
|
| Methods defined here:
|
| xview(self, *args)
| Query and change the horizontal position of the view.
|
| xview_moveto(self, fraction)
| Adjusts the view in the window so that FRACTION of the
| total width of the canvas is off-screen to the left.
|
| xview_scroll(self, number, what)
| Shift the x-view according to NUMBER which is measured in "units"
| or "pages" (WHAT).
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
3.4 查看某个包/模块下的某个类下的某个函数的帮助信息
如,通过执行help('tkinter.XView.xview')查看tkinter包下的XView类下的xview()函数的帮助信息:
>>> help('tkinter.XView.xview')
Help on function xview in tkinter.XView:
tkinter.XView.xview = xview(self, *args)
Query and change the horizontal position of the view.
3.5 查看本机Python所有已安装的模块
通过执行help('modules')查看本机Python所有已安装的模块:
>>> help('modules')
Please wait a moment while I gather a list of all available modules...
IPython binascii mailcap squeezer
MySQLdb binhex mainmenu sre_compile
PIL bisect markupsafe sre_constants
......................................................................
base64 macosx sqlite3 zzdummy
bdb mailbox sqlparse
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".
3.6 查看本机python所有模块中包含指定字符串的模块
如,通过执行help('modules ttk')查看本机Python所有模块中在模块名或概述中包含指定字符串'ttk'的模块:
>>> help('modules ttk')
Here is a list of modules whose name or summary contains 'ttk'.
If there are any, enter a module name to get more help.
test.test_ttk_guionly
test.test_ttk_textonly
tkinter.test.test_ttk
tkinter.test.test_ttk.test_extensions
tkinter.test.test_ttk.test_functions
tkinter.test.test_ttk.test_style
tkinter.test.test_ttk.test_widgets
tkinter.ttk - Ttk wrapper.
3.7 查看Python所有的关键字
通过执行help('keywords')查看Python所有的关键字:
>>> help('keywords')
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
3.8 查看常见的主题
通过执行help('topics')查看Python常见的主题:
>>> help('topics')
Here is a list of available topics. Enter any topic name to get more help.
ASSERTION DELETION LOOPING SHIFTING
ASSIGNMENT DICTIONARIES MAPPINGMETHODS SLICINGS
ATTRIBUTEMETHODS DICTIONARYLITERALS MAPPINGS SPECIALATTRIBUTES
ATTRIBUTES DYNAMICFEATURES METHODS SPECIALIDENTIFIERS
AUGMENTEDASSIGNMENT ELLIPSIS MODULES SPECIALMETHODS
BASICMETHODS EXCEPTIONS NAMESPACES STRINGMETHODS
BINARY EXECUTION NONE STRINGS
BITWISE EXPRESSIONS NUMBERMETHODS SUBSCRIPTS
BOOLEAN FLOAT NUMBERS TRACEBACKS
CALLABLEMETHODS FORMATTING OBJECTS TRUTHVALUE
CALLS FRAMEOBJECTS OPERATORS TUPLELITERALS
CLASSES FRAMES PACKAGES TUPLES
CODEOBJECTS FUNCTIONS POWER TYPEOBJECTS
COMPARISON IDENTIFIERS PRECEDENCE TYPES
COMPLEX IMPORTING PRIVATENAMES UNARY
CONDITIONAL INTEGER RETURNING UNICODE
CONTEXTMANAGERS LISTLITERALS SCOPING
CONVERSIONS LISTS SEQUENCEMETHODS
DEBUGGING LITERALS SEQUENCES
3.9 查看内置的类型的帮助信息
如,通过执行help('str')查看内置的str类型的帮助信息:
3.10 查看类型的成员方法的帮助信息
如,通过执行help('str.find')查看内置的str类型的find()方法的帮助信息:
>>> help('str.find')
Help on method_descriptor in str:
str.find = find(...)
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
3.11 将帮助信息存入指定路径文件名中
如将tkinter包的帮助信息存入指定路径:C:/MyHelp/tkinter_help.txt
>>> import os, sys, string
>>> sys.stdout=open('c:/MyHelp/tkinter_help.txt', 'a')
>>> help('tkinter')
>>> sys.stdout.close()
>>>
如将tkinter.ttk模块的帮助信息存入指定路径:C:/MyHelp/tkinter.ttk_help.txt
>>> import os, sys, string
>>> sys.stdout=open('c:/MyHelp/tkinter.ttk_help.txt', 'a')
>>> help('tkinter.ttk')
>>> sys.stdout.close()
>>>
结束语
希望本次分享的对Python内置help()函数的介绍能对您有所帮助! 喜欢的话就点个赞加关注支持一下哈:)