我要如何改变Tkinter的一个框架的背景是什么

我要如何改变Tkinter的一个框架的背景是什么

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

问题描述

我一直在创造一个电子邮件的使用Tkinter的,在Python 3.3的程序。
各种网站上我已经看到了框架构件可以用 Frame.config(背景=色)获得不同的背景
然而,当我使用这在我的框架它提供了以下错误:

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

我不明白。
我会后我的整个源$ C ​​$ C,但我不希望它暴露在互联网上,但框架建立是这样的:

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

我曾尝试多种选择要修改的背景。框架是围绕像电子邮件preVIEW一个包裹的收件箱。

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:\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"

提供了以下错误从答案code:

Gives the following error with the code from the answer:

  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


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


Solved! Thanks. Its the inbox bar to the right, background needed to be white.

推荐答案

这个问题的根源是,你在不知不觉中使用类从 TTK 包,而不是从 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 global imports -- you can overwrite the definition of classes and commands.

我建议做进口是这样的:

I recommend doing imports like this:

import tkinter as tk
import ttk

然后preFIX与小部件或者 TK TTK

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

然后,它立即变得很明显,你所使用的小工具,只是一点点更多的输入为代价的。如果你已经做到了这一点,这个错误在code永远不会发生。

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

07-29 16:20