【问题标题】:How do I change the background of a Frame in Tkinter?如何在 Tkinter 中更改 Frame 的背景?
【发布时间】:2024-01-08 01:27:01
【问题描述】:

我一直在使用 Python 3.3 中的 Tkinter 创建一个 Email 程序。 在各种网站上,我看到 Frame 小部件可以使用 Frame.config(background="color") 获得不同的背景。 但是,当我在我的框架中使用它时,会出现以下错误:

_tkinter.TclError: unknown option "-Background"

执行以下操作时不起作用:

frame = Frame(root, background="white")

或者:

frame = Frame(root)
frame.config(bg="white")

我想不通。 我会发布我的整个源代码,但我不希望它暴露在互联网上,但是框架创建是这样的:

mail1 = Frame(self, relief=SUNKEN)
mail1.pack()
mail1.place(height=70, width=400, x=803, y=109)
mail1.config(Background="white")

我尝试了多个选项来尝试修改背景。该框架就像一个环绕收件箱的电子邮件预览。

如果需要,这是我导入模块的方式:

import tkinter, time, base64, imaplib, smtplib
from imaplib import *
from tkinter import *
from tkinter.ttk import *

以下是完整的回溯:

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"

使用答案中的代码给出以下错误:

  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

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

【问题讨论】:

  • 混合使用placepackgrid 方法是个坏主意。
  • 好的,谢谢,我使用它是因为有人在 Tkinter 教程中使用它,但从那以后它就卡住了。我将优化我的代码以仅使用地点。
  • 在 99% 的情况下最好使用 placegrid
  • @kalgasnik 是的,我发现在使用 'code'pack'code' 时,我无法创建我想要的界面。
  • @kalgasnik:我认为你给出了一些不好的建议。 place 很少是正确的选择; packgrid 通常都比 place 提供更好的结果。此外,可以将它们混合在一个应用程序中,但不能将它们混合在同一个容器小部件中。

标签: python python-3.x background tkinter frame


【解决方案1】:

问题的根源在于您在不知不觉中使用了ttk 包中的Frame 类,而不是tkinter 包中的类。 ttk 的那个不支持背景选项。

这是您不应该进行通配符导入的主要原因——您可以覆盖类和命令的定义。

我建议像这样进行导入:

import tkinter as tk
import ttk

然后在小部件前面加上 tkttk

f1 = tk.Frame(..., bg=..., fg=...)
f2 = ttk.Frame(..., style=...)

然后,您使用的小部件会立即变得明显,而只需多输入一点点。如果你这样做了,你的代码中的这个错误就不会发生了。

【讨论】:

  • 谢谢!将来会使用它。通常我使用import tkinter as tk,但我把它留给了 tkinter,因为我正在将我的程序从松散函数转换为基于类的程序。
  • @N.McA.:如果你进行全局导入,它错误的来源,这就是我给出这个建议的原因。有了良好的编程习惯,这不是问题。
  • 很棒的答案,很好的收获。
【解决方案2】:

您使用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()

【讨论】:

  • 是的,我的窗口是 1000 宽。
  • 不确定评论部分的格式如何工作,在 OP 中发帖
  • @kalgasnik:您在评论中写道“带有 'bg' 的代码工作正常(python2.7 和 3.3)。”这是真的,但你错过了这个答案的重点。当您使用来自 tkinter 的标准 Frame 时,它可以工作,但 OP 是(意外地?)使用来自 ttk 包的那个,它不起作用。
  • @BryanOakley 我对编程很陌生,所以我想这是一个意外是的。它现在可以工作了。另外据我了解,如果您使用Style,您将不能再使用bg="white" 之类的东西,对吧?因为我在一些标签上尝试过,它们给出了同样的错误。
最近更新 更多