Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

python - How do I change the background of a Frame in Tkinter?

I have been creating an Email program 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:

_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:UsersWesselDropboxPythonMainClass Ginomail.py", line 457, in <module>
main()
File "C:UsersWesselDropboxPythonMainClass Ginomail.py", line 453, in main
app = Application(root) #start the application with root as the parent
File "C:UsersWesselDropboxPythonMainClass Ginomail.py", line 60, in __init__
self.initINBOX()
File "C:UsersWesselDropboxPythonMainClass Ginomail.py", line 317, in initINBOX
mail1.config(bg="white")
File "C:Python33libkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:Python33libkinter\__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:UsersWesselDropboxPythonMainClass Ginomail.py", line 317, in initINBOX
  mail1 = Frame(self, relief=SUNKEN, style='myframe')
  File "C:Python33libkintertk.py", line 733, in __init__
  Widget.__init__(self, master, "ttk::frame", kw)
  File "C:Python33libkintertk.py", line 553, in __init__
  tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:Python33libkinter\__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. Happy with the results, lets work on that inbox scrolling.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The root of the problem is that you are unknowingly using the Frame class from the ttk package rather than from the tkinter package. The one from ttk does not support the background option.

This is the main reason why you shouldn't do wildcard 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 :

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...