Module 'tkinter' has no attribute 'Tk' - python

Tkinter doesnt contain any tk attribute.
import tkinter
window = tkinter.Tk()
win.mainloop()
While running this code it gives me an error saying
module 'tkinter' has no attribute 'Tk'

Did you named your python file tkinter.py or Tkinter.py ? Try to rename it. It may be the cause.
if the file name is tkinter.py in program
import tkinter
it will imports the our file name which is overriders the content there is not Tk() module, so it throw the error

Python 3.x
import tkinter
window = tkinter.Tk()
window.mainloop()

import tkinter
raiz= tkinter.Tk()
raiz.mainloop()
remember that the file name cannot be tkinter.py

Try copying the file to the Python path in C drive (in my case)
And the folder should not contain any other file named Tkinter.py or similar for Code click here

try Tk instead of tk
it worked for me, if you think you are importing wrong,try:
import tkinter
tkinter._test()

In my case, the error occurred in top =tk.Tk()
The simple trick I used was to change the uppercase K in 'TK' to lowercase k
import tkinter as tk
import tkinter.filedialog as fd
from tkinter import *
import PIL
from PIL import ImageTk
from PIL import Image
top =tk.Tk()
top.geometry('800x600')
top.title('Image Processing')
top.configure(background='#CDCDCD')

Your python script name is must not be tkinker.py python could prioritize your script as tkinker and that couldn return that error.

it's capital 'T' and small 'k' =>> 'Tk' not capital K make sure, small mistake

Related

Hi i have a 'str' object has no attribute 'get' problem in python-tkinter

it doesn't work
i want it to get a number then click those button bellow and then get the result in the message box what should i do?!
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
win=Tk()
#here is my problem
def household():
global s
global math
math="multiply"
x=int(E.get())
s=(x/100)*500
b="your bill is:"+str(s)
messagebox.showinfo("result",b)
def commercial():
global s
x=int(E.get())
if x<=4000000:
s=(x/100)*750
household()
commercial()
E=Entry(win,bg="#87CEFA")
b1=Button(win,text="Household",bg="#4169E1",command=household)
b3=Button(win,text="commercial",bg="#4169E1",command=commercial)
E.place(x=100,y=85,width=100,height=20)
b1.place(x=100,y=165,width=100,height=30)
b3.place(x=150,y=165,width=100,height=30)
You're calling household() and commercial() before you define E. Because you are doing a wildcard import (from tkinter import *) you're importing the constant E from tkinter, which is defined as the string "e".
The solution is:
don't do wildcard imports import tkinter with import tkinter as tk and then use tk. as a prefix for all tkinter objects (tk.Entry(...), tk.Button(...), etc)
Make sure your code runs in the correct order. If you have functions that depend on a variable, make sure those functions aren't called before the variable is defined.

Tkinter module error on attributes like Tk or Frame

i am trying to use Tkinter on python 3.6
However it gives me an error when i try to run the following code:
import tkinter
top = tkinter.Tk()
top.mainloop()
The error says:
AttributeError: module 'tkinter' has no attribute 'Tk'
I receive the same error if i try to call the attribute Frame.
Any idea?
Thanks
Make sure you didn't name the file tkinter.py; It prevents import of tkinter module because the file your wrote searched first and imported.
Rename the file, and also make sure there's no tkinter.pyc remained.

Python tkinter 8.5 import messagebox

The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Why does IDLE not need the explicit import statement but elsewhere it is required?
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn't import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.
messagebox.showinfo is defined inside tkinter/showinfo.py but when you use from tkinter import * you only import tkinter/__init__.py which holds the definitions of Label, Entry, Button, ... That is how python imports work.
When you use from tkinter import messagebox it looks for messagebox inside tkinter/__init__.py but it can't find it so it tries to import tkinter/messagebox.py
As for the IDLE anomaly, it is a bug in IDLE and I believe that it was patched.

python 2.7 - no module named tkinter

i am on mac os x 10.8, using the integrated python 2.7.
i try to learn about tkinter with tutorials like this for python 2.7 (explicitly not 3)
they propose the following code:
from tkinter import *
import tkinter.messagebox
however, this brings up the error:
ImportError: No module named tkinter
using import.Tkinter with a capital t seems to work, but further commands like
import Tkinter.messagebox
don't (neither does tkinter.messagebox).
I've had this problem with lots of tutorials. what's the thing with the capital / non-capital "T", and how do i get my python to work like it does in the tutorials? Thanks in advance!
Tkinter (capitalized) refers to versions <3.0.
tkinter (all lowecase) refers to versions ≥3.0.
Source: https://wiki.python.org/moin/TkInter
In Tkinter (uppercase) you do not have messagebox.
You can use Tkinter.Message or import tkMessageBox
This code is an example taken from this tutorial:
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def hello():
tkMessageBox.showinfo("Say Hello", "Hello World")
B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()
top.mainloop()
Your example code refers to a python installation >= py3.0. In Python 3.x the old good Tkinter has been renamed tkinter.
For python 2.7 it is Tkinter, however in 3.3.5 it is tkinter.
For python 2.7 use Cap Letters Tkinter but for >3.0 use small letter tkinter

Python Tkinter - global name Tk() is not defined

I've been following a tutorial for the tkinter interface in python and it uses the following piece of code to declare a root widget for the program which then has children widgets:
root = Tk()
I'm getting the following error when trying to interpret this piece of code:
Global name Tk() is not defined
I'm fairly sure this is because tkinter has changed since the tutorial; I cannot find any other tutorials that do not use snippets of code like this so they wouldn't work either.
The question I have is in context simple, but searching, I cannot find the answer;
How can I bypass this: what has changed to the syntax of tkinter and what is the new method of sorts to declare a root widget? additionally it would be brilliant if anybody has the knowledge of tkinter to warn me whether the way in which you can add children widgets to the root has changed as well.
Thank you for any and all replies ~ Michael
You probably forgot from Tkinter import * at the top.
Alternatively, there's
import Tkinter
or
import Tkinter as tk
Edit: Generally, you want to use the idiom from <library> import <module>, so for your specific example from Tkinter import Tk would work.
which just allows you type tk.Button, for example, rather than Tkinter.Button throughout your code. And if you're using Python 3.x, the library is lowercase.
import tkinter
For general importing questions, I've seen the Importing Python Modules link referenced a lot on SO.
You seem to have named the file tkinter.py. You cannot name a file with the module you are importing. Python will try to import from your existing file instead of tkinter module. There will be module name collison. Try to rename your file name.
from Tkinter import *
root = Tk()

Categories