Python Forum
tk is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: tk is not defined (/thread-35998.html)



tk is not defined - jip31 - Jan-08-2022

Hi

The code below works when I execute it with idle or in CLI but when I execute it with vscode, I have the message "NameError: name 'Tk' is not defined"
from tkinter import *
fenetre = Tk()

The name of my py file is well different than "tkinter.py"
What is wrong please?


RE: tk is not defined - buran - Jan-08-2022

Don't name your file tkinter.py It shadows the tkinter module you want to use.


RE: tk is not defined - Gribouillis - Jan-08-2022

Try this to see what gets imported
import tkinter
print(tkinter)



RE: tk is not defined - Larz60+ - Jan-08-2022

tk is a nickname for tkinter when defined as such:
import tkinter as tk

thus your Tk() statement would be fenetre = tk.Tk()

or you can say:
from tkinter import Tk
and then fenetre = Tk() would be valid


RE: tk is not defined - jip31 - Jan-09-2022

It is what I have don elike explained in my email....


RE: tk is not defined - jip31 - Jan-09-2022

(Jan-08-2022, 09:28 AM)Larz60+ Wrote: tk is a nickname for tkinter when defined as such:
import tkinter as tk

thus your Tk() statement would be fenetre = tk.Tk()

or you can say:
from tkinter import Tk
and then fenetre = Tk() would be valid

unfortunately no (see screenshot)


RE: tk is not defined - Gribouillis - Jan-09-2022

The error message doesn't match your code on the screenshot. The code says fenetre = Tk() and the error message says fenetre = tk(). It is not possible.


RE: tk is not defined - jip31 - Jan-12-2022

(Jan-09-2022, 07:43 AM)Gribouillis Wrote: The error message doesn't match your code on the screenshot. The code says fenetre = Tk() and the error message says fenetre = tk(). It is not possible.

what does it means?
the issue comes from visual studio?


RE: tk is not defined - tralfazy - Aug-03-2023

(Jan-08-2022, 08:29 AM)buran Wrote: Don't name your file tkinter.py It shadows the tkinter module you want to use.

This helped me. Thanks! I didn't name my file tkinter.py but I had made one previously.
Then my other programs that used tkinter stopped working because of that tkinter.py that was located in the common python folder I was using.
duh!! I can't believe that I made such a dumb mistake. Here I was uninstalling and reinstalling tkinter over and over trying to fix the problem but the only problem was that I had made a local tkinter.py that was being imported instead of python importing the real tkinter.
aye aye aye! Cool