This question already has answers here:
How do I change the background of a Frame in Tkinter?
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
我一直在Python 2.7中使用Tkinter创建一个Frame,并获得了另一个Background!。但是,当我使用此代码时,出现以下错误:

_tkinter.TclError: unknown option "-bg"


以及完整的回溯:

Traceback (most recent call last):
  File "//MCLSERVER4/MCL Sicherung/M.Grbic/Python/Power Cycling Test/pwrMultiCycling.py", line 26, in <module>
    gui = Interface(root)
  File "//MCLSERVER4/MCL Sicherung/M.Grbic/Python/Power Cycling Test/pwrMultiCycling.py", line 20, in __init__
    self.frame = Frame(parent, bg='', colormap='new')
  File "S:\Python27\lib\lib-tk\ttk.py", line 735, in __init__
    Widget.__init__(self, master, "ttk::frame", kw)
  File "S:\Python27\lib\lib-tk\ttk.py", line 555, in __init__
    Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "S:\Python27\lib\lib-tk\Tkinter.py", line 2036, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-bg"


和我的代码:

from Tkinter import *
from ttk import *
import os
from PIL import Image

class Interface:

    def __init__(self, parent):
        self.parent = parent
        parent.title("")
        local_directory = os.path.dirname(os.path.realpath(__file__))
        self.dataname = "/does/not/exist"

        self.frame = Frame(parent, bg='', colormap='new')



if __name__ == '__main__':
    root = Tk()
    gui = Interface(root)
    root.mainloop()

最佳答案

我认为,我有一个解决方案,而我的问题看起来像这样:

import Tkinter
from ttk import *
import os

    class Interface:

        def __init__(self, parent):
            self.parent = parent
            parent.title("")

            self.frame = Tkinter.Frame(parent, bg='', colormap='new')


    if __name__ == '__main__':
        root = Tkinter.Tk()
        gui = Interface(root)
        root.mainloop()

10-04 15:27
查看更多