Reputation: 382
This is my code for my label
from Tkinter import Tk, BOTH, Canvas, Text,END
import Tkinter as tk
from ttk import Frame, Button, Style, Label, Entry
from tkMessageBox import *
from random import *
class Life(Frame): #is indented correctly in my code
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title('MTG LIFE COUNTER')
self.style = Style()
self.style.theme_use('default')
self.pack(fill=BOTH, expand=1)
can = Canvas(self, width=800, height=500, borderwidth=5, background='black')
can.pack()
can.create_rectangle(0, 0, 200, 200, fill="white", outline="white")
self.add = Button(self, text="Add 1", command=self.rollDice)
self.add.place(x=20, y=210)
self.sub = Button(self, text="Subtract 1", command=self.rollDice)
self.sub.place(x=20, y=230)
self.diceRoll = Button(self, text="Dice!", command=self.rollDice)
self.diceRoll.place(x=20, y=250)
self.coin = Button(self, text="Coin", command=self.rollDice)
self.coin.place(x=20, y=270)
color = 'yellow'
l = Label(self, textvariable=color)
self.namet = Label(self, text="What's your name?", bg='black')
self.namet.place(x=215, y=10)
self.v = tk.StringVar()
self.nameBox = Text(self, height=1, width=10)
self.nameBox.place(x=215,y=30)
The error I get is...
Traceback (most recent call last):
File ".\Mtg Life Counter.py", line 64, in <module>
main()
File ".\Mtg Life Counter.py", line 60, in main
app = Life(root)
File ".\Mtg Life Counter.py", line 12, in __init__
self.initUI()
File ".\Mtg Life Counter.py", line 39, in initUI
self.namet = Label(self, text="What's your name?", bg='black')
File "C:\Python27\lib\lib-tk\ttk.py", line 757, in __init__
Widget.__init__(self, master, "ttk::label", kw)
File "C:\Python27\lib\lib-tk\ttk.py", line 555, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
My question is how do I change the background and foreground of a label?
I tried without the -fg
and -bg
options the code works just fine. Tried looking for the correct syntax all say that this is it. All help would be appreciated.
Upvotes: 0
Views: 2797
Reputation: 385980
The problem is that you are using the ttk Label
widget, and it does not support the bg
or ft
options. You can use background
and foreground
instead.
Upvotes: 3