-5

So, I've recently switched from windows to Linux Ubuntu. My norm is using Tkinter for GUI applications. But from the switch i also realized the Tkinter objects aren't recognized.

I.E

#!/usr/bin/env python
import Tkinter
Window = Tkinter.Tk()
Welcome = Label(Window,"Welcome to Dragive")
Welcome.pack()
Window.mainloop()

Gives me the following error:

NameError: name 'Label' is not defined

This feels so ridiculous seeing as how such a simple script cant be interpreted.

Yes, I have installed python-tk after having realized Tkinter doesn't come pre-packged with linux python. (Very annoying actually)

I have tried

from Tkinter import *

but that gives me this error:

    Welcome = Label(Window,"Welcome to Dragive")
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2595, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2089, in __init__
    for k in cnf.keys():
    AttributeError: 'str' object has no attribute 'keys'

All this has been very irritating. Of course in any other instance, this would run in windows. So far linux has given me nothing but trouble in this instance.

Any help would be appreciated, thank you.

2
  • you need to specify the text as text= "...", and either do Tkinter.Label or the import * although I highly recommend the first. Jun 4, 2016 at 16:39
  • Btw, if you were using Windows you would get the exact same error. Jun 4, 2016 at 16:41

2 Answers 2

0

The errors have nothing to do with linux vs windows. The same code would give you the same errors on either platform.

If you import Tkinter with import Tkinter, then you must prefix all tkinter objects with Tkinter.:

Welcome = Tkinter.Label(...)

This is much better than from Tkinter import *, though I prefer import Tkinter as tk so that the prefix is a little shorter (eg: tk.Label(...))

As for the error message AttributeError: 'str' object has no attribute 'keys', this is because you neglected to tell tkinter which attribute to apply the text to. You need to change your code to be like this (notice the "text="):

Welcome = Label(Window,text="Welcome to Dragive")
1
  • Thank you, not sure how I managed to forget the text definition attribute (facepalm) as for using the * of death, I'll stick to import Tkinter as Tk like you mentioned. Again, thank you! Jun 4, 2016 at 22:07
0
 sudo apt-get install python-tk
1
  • I don't think that has anything to do with the issue in the question. Sep 24, 2020 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.