Programming in Python: Getting "name 'Tk' is not defined" only at Command Prompt, works in IDLE [duplicate]
Asked Answered
C

11

8

A question from a beginner just starting with Tkinter. I downloaded it and wrote the tutorial Hello World program, and it ran fine in IDLE. However, when I saved the program and ran it using command prompt, they all returned NameError: name 'tk' is not defined. I also tried going to the main Python command program and manually entering the code, and it worked fine.

It only fails to recognize Tk() when run through command prompt or through double clicking.

I have no idea what could be going on here.

The code is simply the basic Hello World program that all tutorials teach you to write:

from Tkinter import *
root = Tk()
w = Label(root, text="Hello World")
w.pack()

root.mainloop()

Also because I know everybody is going to answer with it, I am not using 3.x and I have tried running the program with calling it "tkinter," it simply doesn't find the module.

Apparently this program works on other people's computers, so it's not a problem with the code itself. Does anyone have any idea what could be causing this issue?

Corundum answered 26/11, 2011 at 2:16 Comment(4)
have you tried calling your program something other than tkinter? How about "myawesometestprogram.py"?Queue
To clarify the program itself is called "coolprogram.py," I meant that I tried calling the module I was importing tkinter rather than Tkinter.Corundum
I was having the same problem and couldn't find any solution until I simply changed this:Plumbum
Had same problem, but @Queue had solution. Thanks!Genagenappe
J
15

The solution that I found is that do not give the name of your file tkinter.

Jayejaylene answered 12/4, 2020 at 20:54 Comment(0)
R
7

Works fine in my computer .

Since You said : NameError: name 'tk' is not defined.

here tk with a small 't'

You might have written

root = tk()

instead of :

root = Tk()

Check Capital 'T'

Recumbent answered 26/11, 2011 at 2:25 Comment(2)
That was a type in me copying down the error message, oops. The actual one was "NameError: name 'Tk' is not defined. Also if it works on your computer that almost definitely means it's a problem with my command line or something.Corundum
this worked for me on Python 3.5.3 on windows 10 machine from tkinter import * root = Tk() root = mainloop()Rhiannonrhianon
M
6

Also check your file name if you created tkinter.py before, then it can also cause same issue. It would be imported first

Mckay answered 6/12, 2018 at 19:30 Comment(0)
B
5

I suppose this is something to do with

  1. the python version one is using
  2. how you basically imported the library

For python 2.x use this

from Tkinter import *

root = Tk()

root.mainloop()

OR

from tkinter import *

root = tkinter()

root = mainloop()
Broadbent answered 11/7, 2016 at 14:11 Comment(0)
F
5

For Python 2.x use:

from Tkinter import * as tk
import Tkinter as tk
root = Tk()

For Python 3 use:

from tkinter import * as tk
import tkinter as tk
root = tk.Tk()
Farrago answered 15/7, 2017 at 10:34 Comment(0)
B
2

The correct program will be as below-

from tkinter import *
root = Tk()
myLabel = Label(root, text="Hello")
myLabel.pack()
root.mainloop()

I believe you are using python 3 or higher version, and the error is NameError: name 'tk' is not defined which is getting at root = tk().

Please change that to root = Tk() and you will be good to go.

Thanks,

Bespoke answered 10/6, 2020 at 21:2 Comment(0)
S
1

Python is distributed with different builds, some of which include Tkinter and some don't.

What your describing is symptomatic of having multiple Python's on your system. When you run IDLE, obviously you're running one with Tkinter installed. The one available at the command-line apparently doesn't. One way to confirm this is to try to launch IDLE from the command-line: python -m idlelib.idle. If IDLE doesn't launch, the Tkinter isn't installed and you will need to find a path to the version that does run IDLE successfully.

Spathic answered 26/11, 2011 at 3:8 Comment(3)
I'm not really sure what I just did but somehow after running IDLE from the command line as pyshell.py my programs now work.Corundum
Nice to hear you're up and running :-)Spathic
is not tkinter in python 3 ?Equivocate
P
1

I was having the same problem and couldn't find any solution until I simply changed this:

from Tkinter import *

to:

from tkinter import *

I don't know the capital T works for other but under Windows 64 bit Python 3.4.1, it should be "tkinter"

Plumbum answered 1/10, 2014 at 18:42 Comment(0)
T
-1

Thought this would help regarding "tk not defined"

from tkinter import *
import tkinter.tkFileDialog
root = tkinter.Tk('Anything you want is displayed')
Tantra answered 12/10, 2017 at 14:57 Comment(0)
L
-1

This will work well for Python 3:

from Tkinter import *
import Tkinter as tk

window = tk.Tk()
window.title("Welcome to LikeGeeks app")
window.mainloop()
Lancers answered 18/11, 2018 at 2:6 Comment(0)
M
-3

from tkinter import *

root = Tk()

import with the lowercase t and use uppercase T in TK

this worked for me

Morocco answered 1/2, 2018 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.