标签`bg`不存在

时间:2016-01-09 18:37:48

标签: python python-2.7 tkinter

这是我的标签代码

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)

我得到的错误是......

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"

我的问题是如何更改标签的背景和前景?

我试过没有-fg-bg选项,代码就可以了。尝试寻找正确的语法都说这就是它。所有帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

问题是您使用的是ttk Label窗口小部件,但它不支持bgft选项。您可以改为使用backgroundforeground