Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tk is not defined
#1
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?
buran write Jan-08-2022, 08:28 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
Don't name your file tkinter.py It shadows the tkinter module you want to use.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Try this to see what gets imported
import tkinter
print(tkinter)
Reply
#4
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
Reply
#5
It is what I have don elike explained in my email....
Reply
#6
(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)
buran write Jan-09-2022, 08:19 AM:
Don't post images of code, error, output, etc. Copy/paste as text with proper BBCode tags.

Attached Files

Thumbnail(s)
   
Reply
#7
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.
Reply
#8
(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?
Reply
#9
(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
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,775 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020