[英]Import _tkinter or tkinter?
All tutorials simply import tkinter
, 所有教程只需import tkinter
,
I am wondering, though, why not import _tkinter
? 我想知道为什么不import _tkinter
? If my understanding is correct, _tkinter
is the actual library in cpython and tkinter
is the interface or API. 如果我的理解是正确的, _tkinter
是CPython的和实际的库tkinter
是接口或API。
I am simply trying to grasp the paradigm as I read through some of the tkinter source code. 当我阅读一些tkinter源代码时,我只是试图掌握范例。 It seems there is some python black magic afoot. 似乎有一些蟒蛇黑魔法正在酝酿之中。
_tkinter
is a C-based module that wraps an internal tcl/tk interpreter. _tkinter
是一个基于C的模块,它包装了一个内部的tcl / tk解释器。 When you import it, and it only, you get access to this interpreter but you do not get access to any of the python classes . 当您导入它时,它只能访问此解释器, 但您无法访问任何python类 。
You certainly can import _tkinter
, but then you would have to recreate all of the python interfaces to the tcl/tk functions. 你当然可以导入_tkinter
,但是你必须重新创建tcl / tk函数的所有python接口。
In python "_" marks a variable is intended for internal use 在python“_”中标记变量是供内部使用的
This convention is defined in PEP 8, but isn't enforced by Python 此约定在PEP 8中定义,但不由Python强制实施
You shouldn't import class/modules/variables starting with "_" due to that nature, the developer should allow a property/setter methods to access those attributes.. 由于这种性质,您不应该导入以“_”开头的类/模块/变量,开发人员应该允许属性/ setter方法访问这些属性。
For python2 use "Tkinter" 对于python2使用“Tkinter”
For python3 use "tkinter" 对于python3使用“tkinter”
http://pep8.org/#descriptive-naming-styles http://pep8.org/#descriptive-naming-styles
According to the documentation , 根据文件 ,
The Tk interface is located in a binary module named
_tkinter
. Tk接口位于名为_tkinter
的二进制模块中。 This module contains the low-level interface to Tk, and should never be used directly by application programmers. 该模块包含Tk的低级接口,绝不应由应用程序员直接使用。
So the somewhat unsatisfying answer to "why not import _tkinter?" 所以对“为什么不导入_tkinter?”有些不满意的答案。 is "because the language developers told us not to". 是“因为语言开发人员告诉我们不要”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.