2017-12-03 359 views
2

我試圖運行給予蟒蛇片段:爲什麼我需要從tkinter模塊顯式導入字體模塊,即使已經使用「*」導入完整模塊?

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行中使用「*」符號導入了字體模塊。請舉一些例子,以便我能夠理解這是如何發生的。

回答

0

答案很簡單:Python不會自動導入所有模塊層次結構,只是因爲您導入了頂層結構。那些做這些的人(例如os,會使os.path可用)必須明確地編寫代碼。

只需添加import tkinter.font,和它的作品

然而,由於IDLE使用Tkinter的本身,它已經進口tkinter.font,因此你認爲你可以逃脫而沒有進口, 我希望這有助於:)

+0

來自java背景,這意味着有兩個進口之間的差異...不是? –

+0

是的,@ SumitPrasad,兩個導入之間有區別,導入* - 表示理想情況下導入所有內容(在本例中不是這樣),導入tkinter.font只導入字體。 – csharpcoder

0

模塊可以具有子模塊和/或函數,變量等。由from <module> import *導入的內容取決於模塊的實現方式。大多數模塊不會自動導入子模塊。
在這種情況下,Tkinter的是主模塊,以及字體是一個子模塊,和Tkinter的不被設計爲自動地導入所有的子模塊。所以當你做from tkinter import *,你抓住所有的功能和變量,但不是子模塊。子模塊必須明確導入才能使用。您可以輸入:

from tkinter import * 
from tkinter import font 

或者你也可以輸入:

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

的區別是你是否要使用或tkinter.font.Font(...

+0

先生,你能告訴我們子模塊究竟意味着什麼嗎? –

+0

查看https://docs.python.org/3.6/tutorial/modules.html以獲取關於模塊和子模塊的完整討論。從本質上講,子模塊就像另一個模塊中的獨立模塊... – Gary02127

+0

在tkinter包(如果我錯誤地認爲tkinter是一個包),如果在tkinter包中有一個** __all __ = [font] **聲明** __ init __。py ** tkinter文件,我可以通過簡單地從tkinter import *寫入來輕鬆導入字體。是不是? –