DerpyLe Squid DerpyLe Squid - 1 year ago 87
Linux Question

Tkinter objects not working on linux

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.


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

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/", line 2595, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/usr/lib/python2.7/lib-tk/", 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.

Answer Source

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