【发布时间】:2024-01-08 01:27:01
【问题描述】:
我一直在使用 Python 3.3 中的 Tkinter 创建一个 Email 程序。
在各种网站上,我看到 Frame 小部件可以使用 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
解决了!谢谢。它右侧的收件箱栏,背景需要是白色的。
【问题讨论】:
-
混合使用
place、pack和grid方法是个坏主意。 -
好的,谢谢,我使用它是因为有人在 Tkinter 教程中使用它,但从那以后它就卡住了。我将优化我的代码以仅使用地点。
-
在 99% 的情况下最好使用
place或grid。 -
@kalgasnik 是的,我发现在使用 'code'pack'code' 时,我无法创建我想要的界面。
-
@kalgasnik:我认为你给出了一些不好的建议。
place很少是正确的选择;pack和grid通常都比place提供更好的结果。此外,可以将它们混合在一个应用程序中,但不能将它们混合在同一个容器小部件中。
标签: python python-3.x background tkinter frame