Python Forum

Full Version: tk is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Don't name your file tkinter.py It shadows the tkinter module you want to use.
Try this to see what gets imported
import tkinter
print(tkinter)
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
It is what I have don elike explained in my email....
(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)
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.
(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?
(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