为什么即使使用"*"导入了整个模块,为什么还需要从TKINTER模块中明确导入字体模块?[英] Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using "*"?

问题描述

我试图运行给出python片段:

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

我得到输出为:

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

以及tkinter窗口

但是,当我尝试执行摘要时,如下所示:

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

我有一个错误:

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

我的怀疑是,为什么我需要明确需要从TKINTER导入字体.如您所见,我已经使用第1行中的"*"符号导入了字体模块.请举一些示例,以便我可以理解发生这种情况的情况.

推荐答案

import *并不导入所有内容.它不导入所有内容的一种方法是不会自动搜索软件包的子模块. font是tkinter软件包的子模块,如果尚未由其他导入加载,from tkinter import *将找不到tkinter.font.

.

其他推荐答案

答案很简单:Python不会自动导入所有模块层次结构,只是因为您导入了顶级层次结构.那些这样做的人(例如OS,将使OS.可用)必须明确编写代码.

只是添加import tkinter.font,它起作用

但是,当idle使用tkinter本身时,它已经导入tkinter.font,因此您认为您可以在没有该导入的情况下逃脱, 我希望这会有所帮助:)

其他推荐答案

模块可以具有子模块和/或功能,变量等.from <module> import *导入的内容取决于模块的实现方式.大多数模块不会自动导入子模块.
在这种情况下, tkinter 是主模块, font 是子模块,而 tkinter 并未设计用于自动导入所有子模块.因此,当您执行from tkinter import *时,您会抓住所有功能和变量,而不是subsodules.必须明确进口子模型.您可以输入:

from tkinter import *
from tkinter import font

,也可以输入:

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

区别是您是否要使用font.Font(...或tkinter.font.Font(...

本文地址:https://www.itbaoku.cn/post/2442498.html