标签“bg"不存在 [英] Label `bg` does not exist

查看:62
本文介绍了标签“bg"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的标签代码

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"

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

My question is how do I change the background and foreground of a label?

我在没有 -fg-bg 选项的情况下尝试过,代码工作得很好.尝试寻找正确的语法都说就是这样.所有帮助将不胜感激.

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.

推荐答案

问题是你使用的是 ttk Label 小部件,它不支持 bgft 选项.您可以使用 backgroundforeground 代替.

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.

这篇关于标签“bg"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆