简体   繁体   English

为什么我需要从 tkinter 模块显式导入字体模块,即使使用“*”导入了完整模块?

[英]Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using “*”?

I have tried to run the give python snippet:我试图运行给 python 片段:

from tkinter import *
from tkinter import font
root = Tk()
list_fonts = list(font.families())
for i in list_fonts:
    print(i)
root.mainloop()

I get the output as:我得到的输出为:

Sitka Display
Sitka Banner
Nirmala UI Semilight
Leelawadee UI
Gadugi
Microsoft New Tai Lue
DokChampa
Segoe UI
Calibri
Miriam
Angsana New
Iskoola Pota
Kartika
Segoe UI Semilight
Vijaya
Nirmala UI
Mongolian Baiti
Microsoft YaHei
@Microsoft YaHei
Microsoft YaHei UI
@Microsoft YaHei UI
Vani
Arial Black
IrisUPC
Batang
@Batang
BatangChe
@BatangChe
Gungsuh
@Gungsuh
GungsuhChe
@GungsuhChe
Gautami
Segoe UI Black
Calibri Light
Cambria
Rod
Georgia
Verdana
Symbol
Euphemia
Raavi
Corbel
Shruti
Consolas
Segoe UI Semibold
Simplified Arabic
Cambria Math
DaunPenh
Nyala
Constantia
Yu Gothic
@Yu Gothic
CordiaUPC
Khmer UI
Aharoni
Microsoft Uighur
Times New Roman
Times New Roman CYR
Times New Roman TUR
Times New Roman CE
Times New Roman Baltic
Times New Roman Greek
Segoe Script
Candara
Ebrima
DilleniaUPC
MS Mincho
@MS Mincho
MS PMincho
@MS PMincho
Browallia New
Segoe UI Light
Segoe UI Emoji
Aldhabi
DFKai-SB
@DFKai-SB
SimHei
@SimHei
Lao UI
Courier New
Courier New CYR
Courier New TUR
Courier New CE
Courier New Greek
Courier New Baltic
Kalinga
Microsoft PhagsPa
Tahoma
EucrosiaUPC
KaiTi
@KaiTi
SimSun
@SimSun
NSimSun
@NSimSun
Meiryo
@Meiryo
Meiryo UI
@Meiryo UI
Sylfaen
Tunga
Urdu Typesetting
Microsoft YaHei Light
@Microsoft YaHei Light
Microsoft YaHei UI Light
@Microsoft YaHei UI Light
Webdings
Plantagenet Cherokee
Gabriola
MS Gothic
@MS Gothic
MS UI Gothic
@MS UI Gothic
MS PGothic
@MS PGothic
Gulim
@Gulim
GulimChe
@GulimChe
Dotum
@Dotum
DotumChe
@DotumChe
Lucida Sans Unicode
Andalus
Leelawadee
FangSong
@FangSong
Yu Mincho Demibold
@Yu Mincho Demibold
David
Miriam Fixed
Impact
Levenim MT
Segoe Print
Estrangelo Edessa
Leelawadee UI Semilight
Microsoft JhengHei
@Microsoft JhengHei
Microsoft JhengHei UI
@Microsoft JhengHei UI
Narkisim
MingLiU-ExtB
@MingLiU-ExtB
PMingLiU-ExtB
@PMingLiU-ExtB
MingLiU_HKSCS-ExtB
@MingLiU_HKSCS-ExtB
Yu Mincho Light
@Yu Mincho Light
Latha
Microsoft Sans Serif
FrankRuehl
MingLiU
@MingLiU
PMingLiU
@PMingLiU
MingLiU_HKSCS
@MingLiU_HKSCS
Myanmar Text
Yu Gothic Light
@Yu Gothic Light
Javanese Text
Microsoft Himalaya
Yu Mincho
@Yu Mincho
Lucida Console
Arabic Typesetting
Microsoft Yi Baiti
MV Boli
Wingdings
MT Extra
Arial Unicode MS
@Arial Unicode MS
Century
Wingdings 2
Wingdings 3
Book Antiqua
Century Gothic
Haettenschweiler
MS Outlook
Tempus Sans ITC
Pristina
Papyrus
Mistral
Lucida Handwriting
Kristen ITC
Juice ITC
French Script MT
Freestyle Script
Bradley Hand ITC
Arial Narrow
Garamond
Monotype Corsiva
Algerian
Baskerville Old Face
Bauhaus 93
Bell MT
Berlin Sans FB
Bernard MT Condensed
Bodoni MT Poster Compressed
Britannic Bold
Broadway
Brush Script MT
Californian FB
Centaur
Chiller
Colonna MT
Cooper Black
Footlight MT Light
Harlow Solid Italic
Harrington
High Tower Text
Jokerman
Kunstler Script
Lucida Bright
Lucida Calligraphy
Lucida Fax
Magneto
Matura MT Script Capitals
Modern No. 20
Niagara Engraved
Niagara Solid
Old English Text MT
Onyx
Parchment
Playbill
Poor Richard
Ravie
Informal Roman
Showcard Gothic
Snap ITC
Stencil
Viner Hand ITC
Vivaldi
Vladimir Script
Wide Latin
Tw Cen MT
Tw Cen MT Condensed
Script MT Bold
Rockwell Extra Bold
Rockwell Condensed
Rockwell
Rage Italic
Perpetua Titling MT
Perpetua
Palace Script MT
OCR A Extended
Maiandra GD
Lucida Sans Typewriter
Lucida Sans
Imprint MT Shadow
Goudy Stout
Goudy Old Style
Gloucester MT Extra Condensed
Gill Sans Ultra Bold Condensed
Gill Sans Ultra Bold
Gill Sans MT Condensed
Gill Sans MT
Gill Sans MT Ext Condensed Bold
Gigi
Franklin Gothic Medium Cond
Franklin Gothic Heavy
Franklin Gothic Demi Cond
Franklin Gothic Demi
Franklin Gothic Book
Forte
Felix Titling
Eras Medium ITC
Eras Light ITC
Eras Demi ITC
Eras Bold ITC
Engravers MT
Elephant
Edwardian Script ITC
Curlz MT
Copperplate Gothic Light
Copperplate Gothic Bold
Century Schoolbook
Castellar
Calisto MT
Bookman Old Style
Bodoni MT Condensed
Bodoni MT Black
Bodoni MT
Blackadder ITC
Arial Rounded MT Bold
Agency FB
Bookshelf Symbol 7
MS Reference Sans Serif
MS Reference Specialty
Berlin Sans FB Demi
Tw Cen MT Condensed Extra Bold

As well as a tkinter window以及 tkinter 窗口

But when I tried to execute the snippet as given below:但是当我尝试执行下面给出的代码段时:

from tkinter import *
root = Tk()
list_fonts = list(font.families())
for i in list_fonts:
    print(i)
root.mainloop()

I get an error:我收到一个错误:

Traceback (most recent call last):
  File "fonts.py", line 4, in <module>
    list_fonts = list(font.families())
NameError: name 'font' is not defined

My doubt is that why do I explicitly need to import font from tkinter.我的疑问是为什么我明确需要从 tkinter 导入字体。 As you can see that I have already imported the font module using the "*" notation in line 1. Please give some example so that I can understand how this is taking place.如您所见,我已经使用第 1 行中的“*”符号导入了字体模块。请举例说明,以便我了解这是如何发生的。

import * does not import everything. import *不会导入所有内容。 One way that it doesn't import everything is that it does not automatically search for submodules of a package .它不导入所有内容的一种方式是它不会自动搜索 package 的子模块 font is a submodule of the tkinter package, and if it has not already been loaded by some other import, from tkinter import * will not find tkinter.font . fonttkinter包的子模块,如果它还没有被其他导入加载, from tkinter import * tkinter.font from tkinter import *将找不到tkinter.font

The answer is simple: Python doesn't automagically import all module hierarchies, just because you import the top-level one.答案很简单:Python 不会自动导入所有模块层次结构,仅仅因为您导入了顶级层次结构。 Those who do (eg os, which will make os.path available) have to explicitly write code for that .那些这样做的人(例如 os,这将使 os.path 可用)必须明确地为此编写代码。

Just add import tkinter.font , and it works只需添加import tkinter.font ,它就可以工作

However, as IDLE uses tkinter itself, it has already imported tkinter.font, thus you think you can get away without that import , I hope this helps : )但是,由于 IDLE 使用 tkinter 本身,它已经导入了 tkinter.font,因此您认为没有导入就可以逃脱,我希望这会有所帮助:)

Modules can have submodules and/or functions, variables, etc. What gets imported by from <module> import * depends on how the module was implemented.模块可以有子模块和/或函数、变量等。 from <module> import *取决于模块的实现方式。 Most modules will not automatically import submodules.大多数模块不会自动导入子模块。
In this case, tkinter is the main module, and font is a submodule, and tkinter was not designed to import all submodules automatically.在这种情况下, tkinter是主模块, font是一个子模块, tkinter 没有被设计为自动导入所有子模块。 So when you do from tkinter import * , you are grabbing all the functions and variables, but not the submodules.因此,当您执行from tkinter import * ,您将获取所有函数和变量,而不是子模块。 The submodules must be explicitly imported to be used.子模块必须显式导入才能使用。 You can enter:您可以输入:

from tkinter import *
from tkinter import font

Or you could also enter:或者你也可以输入:

import tkinter  # actually not needed since the below "does both"
import tkinter.font

The difference would be whether you want to use font.Font(... or tkinter.font.Font(...区别在于您是否要使用font.Font(...tkinter.font.Font(...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我必须使用python中的电子邮件模块“从模块导入”? - Why do I have to “from module import” with the email module in python? 为什么需要使用'import'语句显式导入*内置* python模块'sys'? - why does the *built-in* python module 'sys' need to be explicitly imported with an 'import' statement? Python模块导入 - 为什么组件仅在显式导入时可用? - Python module import - why are components only available when explicitly imported? 如何在Python 2中显式从同一目录导入模块? - How do I import a module from the same directory explicitly in Python 2? python importlib.import_module 需要显式导入的模块 - python importlib.import_module requires explicitly imported module 修改使用from ... import *导入的模块中的变量 - Modifying a variable in a module imported using from … import * 当本地模块屏蔽由外部模块导入的标准库模块时,如何导入外部模块? - How do I import an external module when a local module masks a standard library module imported by the external module? 如何重新加载使用`from module import *`导入的python模块 - How to reload python module imported using `from module import *` 我需要知道tkinter import *和tkinter import&#39;module&#39;之间的区别是什么 - i need to know whats the diference between from tkinter import * and from tkinter import 'module' Python 从导入模块导入模块问题 - Python import module from imported module issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM