tkinter – Programming in Python: Getting name Tk is not defined only at Command Prompt, works in IDLE

tkinter – Programming in Python: Getting name Tk is not defined only at Command Prompt, works in IDLE

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

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()

tkinter – Programming in Python: Getting name Tk is not defined only at Command Prompt, works in IDLE

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()

Leave a Reply

Your email address will not be published. Required fields are marked *