Labels not defined in tkinter app

Question:

I’m trying to make a basic window with the text “t” inside using Tkinter, however when running the code the shell spits out “NameError: name ‘Label’ is not defined”. I’m running Python 3.5.2.

I followed the tutorials but the problem is in the label = Label(root, text="test") line.

import tkinter

root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)

label = Label(root, text="test")
label1.pack()

root = mainloop()

Is the label function different in 3.5.2?

Asked By: AgentL3r

||

Answers:

You never imported the Label class. Try tkinter.Label

Check the import statements for those tutorials

Maybe they imply from tkinter import *

Answered By: OneCricketeer
import tkinter

root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)

label = tkinter.Label(root, text="test")
label1.pack()

root = tkinter.mainloop() # <- prob need to fix this as well.

Because you didn’t do from tkinter import * you need to invoke the Label from the tkinter module.

Alternatively you can do:

from tkinter import *
...
label = Label(root, text="test")
Answered By: Torxed

stumbled across the same Problem. Most beginners guides seem to mess up here.
I had to use a second line in the configuration:

!/usr/bin/python3

import tkinter
from tkinter import *

Answered By: 88250

On Windows Operating System your code should run well but on macOS, you will get a problem. I don’t know why something like that happen. Anyway try:

import tkinter, 
from tkinter import*

And run

After that just write:

from tkinter import *

or

import tkinter 

(not both this time)

Answered By: sadric adigbe

It’s a typo error…
Nothing to do with the import statements.

Label = with a capital L not l

Label(root, text="Username").place(x=20,y=20)
capitalize l

Answered By: Shem spice
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.