找不到 tkinter ttk 样式布局

tkinter ttk style Layout not found

以下代码出现错误。继续查看示例并重新评估代码,但无法真正找出不应该 运行 的任何原因。是不是有什么地方不对导致找不到'mRRed'布局? (而且我确定后续)...

import tkinter as tk
from tkinter import ttk

class MainFrame:
    def __init__(self,parent):
        self.frame = ttk.Frame(parent,padding='3 3 12 12')
        self.frame.grid(column=0, row=0)

        self.mRRed = ttk.Style()
        self.mRBlue = ttk.Style()
        self.mPurple = ttk.Style()
        self.mPink = ttk.Style()
        self.mSCyan = ttk.Style()
        self.mVYellow = ttk.Style()
        self.mGreen  = ttk.Style()

        self.mRRed.configure("mRRed",background="#E61E50")
        self.mRBlue.configure("mRBlue",background="#0F69AF")
        self.mPurple.configure("mPurple",background="#503291")
        self.mPink.configure("mPink",background="#EB3C96")
        self.mSCyan.configure("mSCyan",background="#2BDECD")
        self.mVYellow.configure("mVYellow",background="#FFC832")
        self.mGreen.configure("mGreen",background="#149B5F")


        self.toolRibbon    = ttk.Frame(self.frame, style='mRRed')
        self.subtoolRibbon = ttk.Frame(self.frame, style='mRBlue')
        self.titleFrame    = ttk.Frame(self.frame, style='mPruple')
        self.contentFrame  = ttk.Frame(self.frame, style='mPink')
        self.optionRibbon  = ttk.Frame(self.frame, style='mSCyan')
        self.statusFrame   = ttk.Frame(self.frame, style='mVYellow')
        self.infoFrame     = ttk.Frame(self.frame, style='mGreen')

        self.toolRibbon.grid(column=0,row=0)
        self.subtoolRibbon.grid(column=0,row=1)
        self.titleFrame.grid(column=1,row=1)
        self.contentFrame.grid(column=1,row=2)
        self.optionRibbon.grid(column=1,row=3)
        self.statusFrame.grid(column=1,row=4)
        self.infoFrame.grid(column=4,row=1)

root = tk.Tk()
root.title("test")

main = MainFrame(root)
root.mainloop()

--------------------------------------------------------------------------- TclError Traceback (most recent call last) in 43 root.title("test") 44 ---> 45 main = MainFrame(root) 46 root.mainloop()

in init(self, parent) 24 25 ---> 26 self.toolRibbon = ttk.Frame(self.frame, style='mRRed') 27 self.subtoolRibbon = ttk.Frame(self.frame, style='mRBlue') 28 self.titleFrame = ttk.Frame(self.frame, style='mPruple')

D:\Programs\Anaconda\lib\tkinter\ttk.py in init(self, master, **kw) 740 borderwidth, relief, padding, width, height 741 """ --> 742 Widget.init(self, master, "ttk::frame", kw) 743 744

D:\Programs\Anaconda\lib\tkinter\ttk.py in init(self, master, widgetname, kw) 557 # Load tile now, if needed 558 _load_tile(master) --> 559 tkinter.Widget.init(self, master, widgetname, kw=kw) 560 561

D:\Programs\Anaconda\lib\tkinter__init__.py in init(self, master, widgetName, cnf, kw, extra) 2297 del cnf[k] 2298
self.tk.call( -> 2299 (widgetName, self._w) + extra + self._options(cnf)) 2300 for k, v in classes: 2301
k.configure(self, v)

TclError: Layout mRRed not found

ttk.Style() 遵循用户创建样式的 T<widget name> 命名约定。这实际上并不在 python tkinter 文档中,除了他们在示例中使用命名约定但从未解释过的事实。

简而言之,如果您想将样式应用于 Frame 小部件,您需要在样式名称中使用后缀 .TFrame

我也注意到,我误读了你的问题,我的印象是你使用的是 Label 而不是 Frame

我添加了一些 Labels 来演示效果,但在您的示例中,您的代码将是:

import tkinter as tk
from tkinter import ttk

class MainFrame:
    def __init__(self,parent):
        self.frame = ttk.Frame(parent,padding='3 3 12 12')
        self.frame.grid(column=0, row=0)

        self.mRRed = ttk.Style()
        self.mRBlue = ttk.Style()
        self.mPurple = ttk.Style()
        self.mPink = ttk.Style()
        self.mSCyan = ttk.Style()
        self.mVYellow = ttk.Style()
        self.mGreen  = ttk.Style()

        self.mRRed.configure("mRRed.TFrame",background="#E61E50")
        self.mRBlue.configure("mRBlue.TFrame",background="#0F69AF")
        self.mPurple.configure("mPurple.TFrame",background="#503291")
        self.mPink.configure("mPink.TFrame",background="#EB3C96")
        self.mSCyan.configure("mSCyan.TFrame",background="#2BDECD")
        self.mVYellow.configure("mVYellow.TFrame",background="#FFC832")
        self.mGreen.configure("mGreen.TFrame",background="#149B5F")


        self.toolRibbon    = ttk.Frame(self.frame, style='mRRed.TFrame',borderwidth=4)
        self.subtoolRibbon = ttk.Frame(self.frame, style='mRBlue.TFrame',borderwidth=4)
        self.titleFrame    = ttk.Frame(self.frame, style='mPurple.TFrame',borderwidth=4)
        self.contentFrame  = ttk.Frame(self.frame, style='mPink.TFrame',borderwidth=4)
        self.optionRibbon  = ttk.Frame(self.frame, style='mSCyan.TFrame',borderwidth=4)
        self.statusFrame   = ttk.Frame(self.frame, style='mVYellow.TFrame',borderwidth=4)
        self.infoFrame     = ttk.Frame(self.frame, style='mGreen.TFrame',borderwidth=4)

        self.toolRibbon.grid(column=0,row=0)
        self.subtoolRibbon.grid(column=0,row=1)
        self.titleFrame.grid(column=1,row=1)
        self.contentFrame.grid(column=1,row=2)
        self.optionRibbon.grid(column=1,row=3)
        self.statusFrame.grid(column=1,row=4)
        self.infoFrame.grid(column=4,row=1)

        self.test = ttk.Label(self.optionRibbon, text='cyan test')
        self.test.pack()

        self.test2 = ttk.Label(self.contentFrame, text='pink test')
        self.test2.pack()

root = tk.Tk()
root.title("test")

main = MainFrame(root)
root.mainloop()