Python 如何在 Tkinter 中更改框架的背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16639125/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the background of a Frame in Tkinter?
提问by IPDGino
I have been creating an Emailprogram using Tkinter, in Python 3.3.
On various sites I have been seeing that the Frame widget can get a different background using Frame.config(background="color").
However, when I use this in my Frames it gives the following error:
我一直在使用 Python 3.3 中的 Tkinter创建电子邮件程序。在各种网站上,我一直看到 Frame 小部件可以使用Frame.config(background="color"). 但是,当我在 Frames 中使用它时,会出现以下错误:
_tkinter.TclError: unknown option "-Background"
It does not work when doing the following:
执行以下操作时它不起作用:
frame = Frame(root, background="white")
Or:
或者:
frame = Frame(root)
frame.config(bg="white")
I can't figure it out. I would post my whole source code but I dont want it exposed on the internet, but the frame creation goes something like this:
我想不通。我会发布我的整个源代码,但我不希望它在互联网上公开,但框架创建是这样的:
mail1 = Frame(self, relief=SUNKEN)
mail1.pack()
mail1.place(height=70, width=400, x=803, y=109)
mail1.config(Background="white")
I have tried multiple options trying to modify the background. The frame is like a wrap around an email preview for an inbox.
我尝试了多个选项来尝试修改背景。该框架就像是收件箱的电子邮件预览的包装。
In case it's needed, this the way I am importing my modules:
如果需要,这是我导入模块的方式:
import tkinter, time, base64, imaplib, smtplib
from imaplib import *
from tkinter import *
from tkinter.ttk import *
The following is the full traceback:
以下是完整的回溯:
Traceback (most recent call last):
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 457, in <module>
main()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 453, in main
app = Application(root) #start the application with root as the parent
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 60, in __init__
self.initINBOX()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
mail1.config(bg="white")
File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
Gives the following error with the code from the answer:
使用答案中的代码给出以下错误:
  File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
  mail1 = Frame(self, relief=SUNKEN, style='myframe')
  File "C:\Python33\lib\tkinter\ttk.py", line 733, in __init__
  Widget.__init__(self, master, "ttk::frame", kw)
  File "C:\Python33\lib\tkinter\ttk.py", line 553, in __init__
  tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 2075, in __init__
  (widgetName, self._w) + extra + self._options(cnf))
  _tkinter.TclError: Layout myframe not found
Solved! Thanks. Its the inbox bar to the right, background needed to be white.

解决了!谢谢。它的收件箱栏在右边,背景需要是白色的。

采纳答案by Bryan Oakley
The root of the problem is that you are unknowingly using the Frameclass from the ttkpackage rather than from the tkinterpackage. The one from ttkdoes not support the background option.
问题的根源在于您在不知不觉中使用Frame了ttk包中的类而不是tkinter包中的类。一个 fromttk不支持背景选项。
This is the main reason why you shouldn't do global imports -- you can overwrite the definition of classes and commands.
这是您不应该进行全局导入的主要原因——您可以覆盖类和命令的定义。
I recommend doing imports like this:
我建议做这样的导入:
import tkinter as tk
import ttk
Then you prefix the widgets with either tkor ttk:
然后,您使用tkor为小部件添加前缀ttk:
f1 = tk.Frame(..., bg=..., fg=...)
f2 = ttk.Frame(..., style=...)
It then becomes instantly obvious which widget you are using, at the expense of just a tiny bit more typing. If you had done this, this error in your code would never have happened.
然后,您正在使用哪个小部件立即变得一目了然,代价是输入更多一点。如果你这样做了,你的代码中的这个错误就永远不会发生。
回答by kalgasnik
You use ttk.Frame, bgoption does not work for it.  You should create style and apply it to the frame.
您使用ttk.Frame,bg选项对它不起作用。  您应该创建样式并将其应用于框架。
from tkinter import *
from tkinter.ttk import * 
root = Tk()
s = Style()
s.configure('My.TFrame', background='red')
mail1 = Frame(root, style='My.TFrame')
mail1.place(height=70, width=400, x=83, y=109)
mail1.config()
root.mainloop()

