如何在 Tkinter 中更改框架的背景?

问题描述 投票:0回答:3

我一直在使用 Python 3.3 中的 Tkinter 创建一个 Email 程序。 在各个网站上,我发现框架小部件可以使用

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

解决了!谢谢。它是右侧的收件箱栏,背景需要是白色的。 Happy with the results, lets work on that inbox scrolling.

python python-3.x background tkinter frame
3个回答
60
投票

问题的根源在于您在不知不觉中使用了

Frame
包中的
ttk
类,而不是
tkinter
包中的类。
ttk
的不支持背景选项。

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

我建议像这样进行导入:

import tkinter as tk
import ttk

然后在小部件前添加

tk
ttk
:

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

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


18
投票

您使用

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

0
投票

不是 tkinter 经验最丰富的。但是..

import tkinter as tk
app = tk.Tk()

mail1 = app.Frame(root, relief=SUNKEN, background="white", height=70, width=400)
mail1.pack()
mail1.place(x=803, y=109)

app.mainloop()

正在为我工作,请确保你有最新的 python 和 tkinter

© www.soinside.com 2019 - 2024. All rights reserved.