tkinter TclError: unknown option on widget attributes

Viewed 3551

This is my first application on Tkinter, I am trying to take inputs from a selected file button from the dropdown menu and trigger the function which is to be executed when the submit button is clicked. This objective is achieved but designing the buttons and labels as required is failing.

root = tk.Tk()
root.geometry('1200x800')
root.title("Update System")

label_0 = Label(root, text="Update System",width=25,font=("bold", 25),height=2,bg='brown',fg='white')
label_0.place(x=170,y=30)

root.mainloop()

I receive the error TclError: unknown option "-fg" (for bg-background, height attributes of buttons and labels). Complete traceback is shown below:

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-15-2c166bb79a75> in <module>
      3 root.title("Update System")
      4 
----> 5 label_0 = Label(root, text="Update System",width=25,font=("bold", 25),height=2,bg='brown',fg='white')
      6 label_0.place(x=170,y=30)
      7 

~\Anaconda3\lib\tkinter\ttk.py in __init__(self, master, **kw)
    759             relief, text, wraplength
    760         """
--> 761         Widget.__init__(self, master, "ttk::label", kw)
    762 
    763 

~\Anaconda3\lib\tkinter\ttk.py in __init__(self, master, widgetname, kw)
    557             # Load tile now, if needed
    558             _load_tile(master)
--> 559         tkinter.Widget.__init__(self, master, widgetname, kw=kw)
    560 
    561 

~\Anaconda3\lib\tkinter\__init__.py in __init__(self, master, widgetName, cnf, kw, extra)
   2297             del cnf[k]
   2298         self.tk.call(
-> 2299             (widgetName, self._w) + extra + self._options(cnf))
   2300         for k, v in classes:
   2301             k.configure(self, v)

TclError: unknown option "-height"

It would be great if someone can help me understand and figure out this issue. Thanks in advance!

1 Answers

There are two Label widgets in Tk and hence in tkinter. The older standard Tk label supports directly setting things like foreground and background colors, whereas the newer Ttk (“Themed Tk”) does not (but instead supports theming, specifically it supports following the OS native theme very closely). They're both kept because the new one is definitely not a drop-in replacement for the old, and some applications (maybe yours included) need the exact functionality of the old.

If we look in your traceback, we see this:

~\Anaconda3\lib\tkinter\ttk.py in __init__(self, master, **kw)
    759             relief, text, wraplength
    760         """
--> 761         Widget.__init__(self, master, "ttk::label", kw)
    762 
    763 

See the ttk in there (twice!)? That indicates that you're using the Ttk Label. To fix your immediate problem, explicitly use the old one.

# Note: tk.Label and not just Label or ttk.Label
label_0 = tk.Label(root, text="Update System", 
                   width=25, font=("bold", 25), height=2, bg='brown', fg='white')

The other alternative would be to create a new style that you could apply to just that widget to make it stand out the way you want… but that's a lot more work for a simple one-off label.

Related