如何在 Tkinter 中更改 Frame 的背景? [英] How do I change the background of a Frame in Tkinter?

查看:138
本文介绍了如何在 Tkinter 中更改 Frame 的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

或者:

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 *

以下是完整的回溯:

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:Python33lib	kinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:Python33lib	kinter\__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:Python33lib	kinter	tk.py", line 733, in __init__
  Widget.__init__(self, master, "ttk::frame", kw)
  File "C:Python33lib	kinter	tk.py", line 553, in __init__
  tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:Python33lib	kinter\__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.

推荐答案

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

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

然后在小部件前面加上 tkttk :

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.

这篇关于如何在 Tkinter 中更改 Frame 的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆