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

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

问题描述

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

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.

我建议这样做导入:

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 中更改框架的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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