gpujol
gpujol

Reputation: 63

Tkinter: NameError: name 'tk' is not defined

I have been following tutorials of how to create a graphical user interface (GUI), in order to get used to it because I will use it in the future. The majority of tutorials use these commands at the first lines:

from tkinter import *

root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()

If I run this simple code I get the following error:

File

"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in root=tk()

NameError: name 'tk' is not defined

After that I changed my code, so I used something like that:

import tkinter as tk

root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()

Now, the error is the following:

"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in root=tk()

TypeError: 'module' object is not callable

Do you know what's going wrong? Could you help me please?

I'm using Spyder for Python 3.3, but I suppose it isn't a problem.

Upvotes: 0

Views: 3833

Answers (2)

Dave
Dave

Reputation: 1

I've just had a similar problem which I found out was because my Python console window in Spyder was connected to a different .py file that I was working on earlier so I closed it and opened a new python console in Spyder and the problem was gone.

Upvotes: 0

Olav
Olav

Reputation: 576

The tutorials you've seen is probably for Python 2. In Python 3 they've changed the naming conventions. So instead of root = tk() in P2, it's root = Tk() in P3 (Tk() is a class, hence the capital T).

In your second example your should write root = tk.Tk() after the import statement

Upvotes: 2

Related Questions