Hello friends :)
This is my code:

#!/usr/bin/env python3
# -*-coding: utf8-*-


def callback():
    lbl.configure(text = 'button clicked!')


import Tkinter

window = Tkinter.Tk()
window.geometry('300x300')
window.configure(background = 'PeachPuff3')
lbl = Tkinter.Label(window, text = 'Nothing yet!')
btn = Tkinter.Button(window, text = 'add', command = callback).pack()

window.mainloop()

Why it doesn't work?

Recommended Answers

All 7 Replies

Python says why it doesn't work

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'Tkinter'

Nothing is more important than the error message! Don't forget to add it to your posts.

The reason is that between python 2 and python 3, some modules in the standard library were renamed. In particular, uppercase characters were removed from their names and Tkinter became tkinter.

Yes you are right, i should post the error too.
Here is the error:

Traceback (most recent call last):
  File "real.py", line 19, in <module>
    btn = Tkinter.Button(window, text = 'add', command = callback()).pack()
  File "real.py", line 7, in callback
    Label['text'] = 'Good morning'
NameError: global name 'Label' is not defined

Well i'm using python 2.

There is no line Label['text'] = 'Good morning' in the code you posted before. Also this error message is very explicit. You are using a variable named Label which has not been defined. Also this variable name is a very bad choice in a tkinter context where Label is a tkinter class.

Oopss yes sorry, i posted an other error.
This is the exact error:

Traceback (most recent call last):
  File "real.py", line 18, in <module>
    btn = Tkinter.Button(window, text = 'add', command = callback()).pack()
  File "real.py", line 6, in callback
    lbl.configure(text = 'button clicked!')
AttributeError: 'NoneType' object has no attribute 'configure'

Again, your error message is for a modified code.
I think you're forgetting that the pack() method returns None, which means that you must write a separate statement to pack the label if you want to keep a pointer to it. For example, here is a working code snippet

#!/usr/bin/env python
# -*-coding: utf8-*-


def callback():
    lbl.configure(text = 'button clicked!')


import Tkinter

window = Tkinter.Tk()
window.geometry('300x300')
window.configure(background = 'PeachPuff3')
lbl = Tkinter.Label(window, text = 'Nothing yet!')
lbl.pack()
btn = Tkinter.Button(window, text = 'add', command = callback)
btn.pack()
print(lbl, btn)
window.mainloop()

Normally you build a house from the bottom up. Try to understand the basics of Python first and put the windows in later.

Thanks @Gribouillis, it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.