Tkinter module error on attributes like Tk or Frame - python

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.

Related

Module 'tkinter' has no attribute 'Tk'

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

ImportError _tkinter

Traceback (most recent call last):
File "/home/pi/sudoku.py", line 3, in <module>
from _tkinter import Tk, Canvas, Frame, Button, BOTH, TOP, BOTTOM
ImportError: cannot import name 'Tk'
I am trying to program a GUI based sudoku game with tkinter. The tutorial I found is in python2, and i've been working to translate it to python 3. The error I keep getting is that Tk, tk, cannot be imported.
Does anyone know why?
I am NEW to coding and programming and yes i've googled it.
From the python documentation: "The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers."
https://docs.python.org/2/library/tkinter.html
I think what you are looking for is something like this:
from tkinter import Tk, Canvas, Frame, Button, BOTH, TOP, BOTTOM
In the import statement it is enough to just say:
import _tkinter as Tk
This imports everything including Canvas, Frame and Button classes. If you do want to import specific classes you need to specify it with a . like this:
import _tkinter.Canvas as TkCanvas
the alias (Tk or TkCanvas) you can choose yourself. Just pick something short and recognisable, also picking the same one as the tutorial makes the tutorial easyer to follow.
Good luck!

Python 3 Tkinter: NameError with Entry widget: name 'Entry' is not defined

I am writing a GUI on Python 3 using Tkinter, but every time I use Entry(), I get a name error.
I tried a more simpler version of the code, (which is written below), but it still caused a NameError:
import tkinter
top = tkinter.Tk()
e = Entry(top)
e.pack()
top.mainloop()
This is the error I get:
Traceback (most recent call last):
File "/home/pi/gui.py", line 4, in <module>
e = Entry()
NameError: name 'Entry' is not defined
I only recently started coding again, so the answer is probably something extremely simple that I didn't realise was wrong with the code, but thanks for any answers.
You didn't import it. Change your code to:
e = tkinter.Entry(top)
Or import it explicitly:
from tkinter import Entry
You didn't import Entry to the local namespace, so you'll need to access it from the module, which you did import:
e = tkinter.Entry(top)
Since you imported the tkinter module, every tkinter action needs to start with tkinter.[function name].
You could also just add:
from tkinter import [function name]
To import multiple functions you seperate them with a comma.
If you use a lot of functions, it is best to import every function, with
from tkinter import *

AttributeError: Message instance has no attribute 'pack'

I'm using Tkinter for the GUI, and I have one problem:
I try to make a message widget, and when I write:
body = Message(top, bd = 2)
body.pack(side=RIGHT)
I get this error:
body.pack(side=RIGHT)
AttributeError: Message instance has no attribute 'pack'
I dont understand this becaue I checked in some guides and Its allowed to use 'message' this way, as seen here in the example: http://www.tutorialspoint.com/python/tk_message.htm
Is there another way to write this?
There are at least two Message classes in Tkinter. One of them comes from tkMessageBox.Message, and the other one is from Tkinter.Message. The former is a subclass of the Dialog from tkCommonDialog, and since packing a dialog is meaningless, there is no pack method for this case. The later is a Tk widget called message, which is the one you want; being a widget, it makes sense to pack it.
Your complete code mostly like have something in the form (Python 2):
from Tkinter import *
from tkMessageBox import *
The second import shadows the Message class from the first import. To use the Message class you are after, simply change the above code to:
from Tkinter import *
import tkMessageBox
Then adapt your code accordingly.

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