简体   繁体   English

如何更改 Tkinter 中 Frame 的背景?

[英]How do I change the background of a Frame in Tkinter?

I have been creating an Email program using Tkinter, in Python 3.3.我一直在 Python 3.3 中使用 Tkinter 创建一个Email程序。 On various sites I have been seeing that the Frame widget can get a different background using Frame.config(background="color") .在各种网站上,我看到 Frame 小部件可以使用Frame.config(background="color")获得不同的背景。 However, when I use this in my Frames it gives the following error:但是,当我在 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 inte.net, but the frame creation goes something like this:我会发布我的整个源代码,但我不希望它暴露在 inte.net 上,但框架的创建是这样的:

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.该框架就像环绕收件箱的 email 预览。

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.背景需要是白色的。 对结果满意,让我们处理收件箱滚动。

The root of the problem is that you are unknowingly using the Frame class from the ttk package rather than from the tkinter package. 这个问题的根源是,你在不知不觉中使用的Frame从类ttk包,而不是从tkinter包。 The one from ttk does not support the background option. 来自ttk不支持后台选项。

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 tk or ttk : 然后使用tkttk为小部件添加前缀:

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. 如果你这样做了,代码中的这个错误永远不会发生。

You use ttk.Frame , bg option does not work for it. 你使用ttk.Framebg选项不起作用。 You should create style and apply it to the frame. 您应该创建样式并将其应用于框架。

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()

Thank you for this answer.谢谢你的回答。 I had as you say imported Tkinter * and in fact had also imported Frame from tkinter explicitly without realising it.正如你所说的,我已经导入了 Tkinter * 并且实际上还明确地从 tkinter 导入了 Frame 而没有意识到这一点。 So in theory it should have worked but in fact I had used used LabelFrame, again without thinking, which is why it didn't work.所以理论上它应该有效,但实际上我使用过 LabelFrame,再次没有考虑,这就是它不起作用的原因。 Added LableFrame to the import and it worked fine.在导入中添加了 LableFrame 并且运行良好。 Many thanks.非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM