Reputation: 11
I'm trying to do a really basic homework assignment, but can't get past the first step. I try to import Tkinter using "import tkinter" or "from tkinter import *" but neither are work. It says "label" and "button" and such are undefined. I'm sure its an easy fix but I have no idea what I'm doing wrong, as I copied simple code from my textbook just to test it and it's still not working. Here's my code.
from tkinter import *
window = Tk()
label = Label(window, text = "This is a label.")
button = Button(window, text = "Press")
label.pack()
button.pack()
window.mainloop()
But I get the yellow warning signs on lines 1, 3, 4 and 5. Line 1: 'from tkinter import *' used; unable to detect undefined names Lines 3-5: (blank) may be undefined, or defined from star inputs: tkinter
I dont know what I'm doing wrong. I'm sure its simple. And I've searched the web but can't find a solution. I know very little about coding.
Upvotes: 1
Views: 7461
Reputation: 21
I could do it like that:
from tkinter import Tk
raiz = Tk()
raiz.mainloop()
I am using Python 3.7.1
Upvotes: 2
Reputation: 36736
The yellow triangles are warnings, not errors. The Spyder IDE is checking that every callable has been defined. In this case, it see that you are calling Tk()
, Label(...)
, and Button(...)
, but they have not been defined or explicitly imported anywhere in your script.
Spyder knows nothing about what functions, classes, or modules are included in tkinter
, so when you use the line
from tkinter import *
it has no idea what is included in the *
, and it gives you a warning to that effect.
Doing *
imports to the global is not a great practice anyhow, you don't know what is in your namespace. Instead, explicitly import what you are using. If you use the line:
from tkinter import Tk, Label, Button
all of the warnings will vanish.
Upvotes: 3