1

Getting module object not callable on this in python 3.3.2

from tkinter import *
import tkinter as tk

root = tk()
root.geometry('400x400')

2 Answers 2

6

When you do import Tkinter as tk, tk refers to a module. This is no different than if you did import Tkinter, where Tkinter refers to a module. Any previous definition of tk is lost.

Later, when you do root = tk(), you are trying to call the module named tk. This is why you are getting the error TypeError: 'module' object is not callable -- you are calling the tkinter module (because of the ()), which you can't do.

The mistake you are making is that the tkinter module defines a class named Tk, and it is this class that you want to call/instantiate. Change your code to do this:

import tkinter as tk
root = tk.Tk()

Also, you shouldn't mix two imports of the same library -- either do import tkinter as tk (recommended) or from tkinter import * but don't do both.

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

4 Comments

builtins.AttributeError: 'module' object has no attribute 'Tk'
@SkiloSkilo: if you get that error, my guess is that you named your program "tkinter.py" or you have another file named "tkinter.py" that python is loading. Try doing the import, then run the following command to see what is getting loaded: print(tk.__file__)
[evaluate tkinter.py] C:\Users\skilo\Documents\pythonprojects\tkinter.py C:\Users\skilo\Documents\pythonprojects\tkinter.py
@SkiloSkilo: rename that file to something else. Your script is importing that instead of the real tkinter module.
0

I'm pretty sure you want to be calling Tk (or tk.Tk) rather than tk all in lowercase. The first is a name you imported with the from tkinter import * line. The latter is the name you imported the module under with your second import statement.

Comments

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.