0

I'm trying to learn python tkinter but a lot of the names (according to the documentation) in tkinter are giving me errors. I tried in eclipse with pydev and even though the editor tells me "tkinter.font" is fine it gives me an error when i run this:

import tkinter

print(tkinter.font)

AttributeError: 'module' object has no attribute 'font'

If i try this in IDLE it works but i tried some other names in IDLE such as scrolledtext, turtle, dnd and they give me attribute errors.

Can someone please help? this is driving me crazy.

1 Answer 1

2

tkinter.font is a module in the Tkinter package. This means that, normally, it is not imported when you do import tkinter.

To access tkinter.font, you have to explicitly import it:

>>> import tkinter
>>> tkinter.font
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'font'
>>>
>>> import tkinter.font
>>> tkinter.font
<module 'tkinter.font' from 'C:\\Python33\\lib\\tkinter\\font.py'>
>>>

For some reason however, tkinter.font is imported when you do import tkinter in IDLE. I don't know the reason for this, but I would guess that it is for convenience. Regardless, it would probably be best to always import tkinter.font (and similar modules) explicitly so that your code will work both inside and outside of IDLE.

Sign up to request clarification or add additional context in comments.

1 Comment

ohhh, for some reason i forgot that modules are not imported. i done dir(tkinter) in IDLE and seen font so i thought i could use it. I probably would have realised my mistake if IDLE hadn't confused me even more. I thought there was something wrong with my installation. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.