AgentL3r
AgentL3r

Reputation: 43

Labels not defined in tkinter app

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?

Upvotes: 1

Views: 28329

Answers (6)

Shem spice
Shem spice

Reputation: 1

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

Upvotes: -2

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

Label = with a capital L not l

Upvotes: 0

sadric adigbe
sadric adigbe

Reputation: 66

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)

Upvotes: 0

88250
88250

Reputation: 1

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 *

...

Upvotes: 0

Torxed
Torxed

Reputation: 23480

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")

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191681

You never imported the Label class. Try tkinter.Label

Check the import statements for those tutorials

Maybe they imply from tkinter import *

Upvotes: 7

Related Questions