classification
Title: 'module' object has no attribute 'font' when "import tkinter" only.
Type: behavior Stage: resolved
Components: Tkinter Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, zaazbb
Priority: normal Keywords:

Created on 2012-11-10 08:20 by zaazbb, last changed 2012-11-10 13:59 by r.david.murray. This issue is now closed.

Messages (3)
msg175267 - (view) Author: zaazbb (zaazbb) Date: 2012-11-10 08:20
when i use tkinter.font, i meet a error "'module' object has no attribute 'font'". if i add "import tkinter.font", it's no error.

but when i use tkinter,messagebox, it's no error without "import tkinter.messagebox".

and, under the tkinter folder, "messagebox.py" and "font.py" have the same hierarchical filesystem.

why they have different import method?

here is my code:
1)tkinter.font -- only use "import tkinter" meet a error, must add "import tkinter.font".code is below:

import tkinter
#import tkinter.font
root = tkinter.Tk()
ft = tkinter.font.Font(family = 'Fixdsys',size = 20,weight = tkinter.font.BOLD)
tkinter.Label(root,text = 'hello sticky',font = ft ).grid()
root.mainloop()

2)tkinter.messagebox -- only use "import tkinter" is all ok.code is below:

import tkinter
def callback():
    if tkinter.messagebox.askokcancel("Quit", "Do you really wish to quit?"):
        root.destroy()
root = tkinter.Tk()
root.protocol("WM_DELETE_WINDOW", callback)
root.mainloop()


i am a beginner on python. if have a reply, please email me. thinks.
msg175268 - (view) Author: zaazbb (zaazbb) Date: 2012-11-10 08:23
when i use tkinter.font, i meet a error "'module' object has no attribute 'font'". if i add "import tkinter.font", it's no error.

but when i use tkinter,messagebox, it's no error without "import tkinter.messagebox".

and, under the tkinter folder, "messagebox.py" and "font.py" have the same hierarchical filesystem.

why they have different import method?

here is my code:
1)tkinter.font -- only use "import tkinter" meet a error, must add "import tkinter.font".code is below:

import tkinter
#import tkinter.font
root = tkinter.Tk()
ft = tkinter.font.Font(family = 'Fixdsys',size = 20,weight = tkinter.font.BOLD)
tkinter.Label(root,text = 'hello sticky',font = ft ).grid()
root.mainloop()

2)tkinter.messagebox -- only use "import tkinter" is all ok.code is below:

import tkinter
def callback():
    if tkinter.messagebox.askokcancel("Quit", "Do you really wish to quit?"):
        root.destroy()
root = tkinter.Tk()
root.protocol("WM_DELETE_WINDOW", callback)
root.mainloop()


i am a beginner on python. if have a reply, please email me. thanks.
msg175276 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-11-10 13:59
This is a question more suited to be asked on python-list or python-tutor, rather than a bug report.

I'll give you a clue while I'm closing the issue: in the font snippt you reference the font attribute when your module is imported.  In the messagebox snippet, you reference the messagebox attribute inside a callback which is *not* executed when your module is imported.  So while the file hierarchy structure is analogous in the two cases, your *code* is not.  When you close the window, you will see your messagebox attribute error when the callback is called.
History
Date User Action Args
2012-11-10 13:59:09r.david.murraysetstatus: open -> closed

type: compile error -> behavior

nosy: + r.david.murray
messages: + msg175276
resolution: not a bug
stage: resolved
2012-11-10 08:23:43zaazbbsetmessages: + msg175268
2012-11-10 08:20:23zaazbbcreate