About this question
Hi. I am trying to write a python code with tkinter. This is my code:
import Tkinter as Tkroot = Tk()And I am getting this error: root = Tk() TypeError: 'module' object is not callable
How to solve this?
Hi. I am trying to write a python code with tkinter. This is my code:
import Tkinter as Tkroot = Tk()And I am getting this error: root = Tk() TypeError: 'module' object is not callable
How to solve this?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Jul 14, 2021
This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line. You are importing a module, not a class. This happened because the module name and class name have the same name. Tk() is a module and not a method. You have to call the Tk() method from the tkinter module. Try this:
import Tkinter as tk
root = tk.Tk()This will work!
Note:
In Python, a script is a module, whose name is determined by the filename. So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure. In Python, everything (including functions, methods, modules, classes etc.) is an object, and methods are just attributes like every other. So, there are no separate namespaces for methods. So when you set an instance attribute, it shadows the class attribute by the same name. The obvious solution is to give attributes of different names.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…