未找到 Ttk 样式布局如何解决?

Ttk style layout not found how to fix?

我正在制作自己的定制 Menubar.My 工作正在进行中,但我最近遇到了一个困惑。 这是我的代码:-

from tkinter import *
from tkinter.ttk import Style,Frame as fp
class menu():
    def __init__(self,parent,bg="#ffffcc"):
        self.parent = parent
        self.bg = bg
        #this is a image in base 64 encoded format
        borderImageData='''
                            iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAABmJLR0QA/wD/
                            AP+gvaeTAAACZ0lEQVRoge2aPU/TURSHn/MvpbYYKa9qQQnlTSOJRBzoYBDi
                            YEwcdPFD+PIFHHRxcCXxO7gxmBgXSTcxMYQEE4spoEWIvBcDBfp2HVqlxRhK
                            TDwtuc947vJ7cnPvGc6R/v5+KhlHO8C/YgW0sQLaWAFtqg4WxKEjJMGQaQyK
                            txbHpZGqmGzG7GzKyoyZfcfsOCZbeChFjaz+vAw/oq6V1K75HiERJ5v+33H/
                            xKnC55czF3CfYD1mxkbY+Pb7sECguUtuPQbMh5d8eksmpZL2r7jccvEGV++B
                            Ma+fsRzNlwOBAID3lNx+gjHm1VNiEweuqSwwWZajxCak85oEQ3wOk0my/4j7
                            7uD1m7ERNuY1Ux7KeozwC3x+uXI3V3AAXFXSPcjCFAtTmuFKw8xPsviRrsHc
                            B+MANHVS7WPuvXK0kjGz43hqaOogL3CyASC+oJrqKMQXAWoayQmI2wuY1J5q
                            qKOQ2gXE4+MYdGIroI0V0MYKaGMFtLEC2lgBbayANlZAGyugjRXQxgpoYwW0
                            sQLaWAFtrIA2DmCSCQC3RzlL6VR7AZIJ8jewtQaIv0Uz05GoPQuYrVXyAqsz
                            JBO0D+imKh0JhtjbYmWGvEAmbabDtPTSelk5Wimc6yNwiekw2Qz7j3hylERc
                            hh9S36YZ7jCkoU2GHpjtdTM5mqv8mtSnkyxFpHtQeoZIJ1n7UnbDelc1vTfl
                            +n1EePOcH0u5cvGyR11r/hJSeyxFzPZGmSx7SE09p3twe1j7asZGCifCcnDt
                            UhzaBwgOSHMnZbNuw86mWY7K3Lg5ZN2mAjkWnbiisQLaWAFtKl7gJzIjtMOb
                            uqQwAAAAAElFTkSuQmCC
                            '''         
        self.borderImage = PhotoImage( data=borderImageData,master=self.parent)
        self.TP_style=Style()
        self.TP_style.element_create("RoundedFrame",
                     "image", self.borderImage,
                     border=14, sticky="nsew")
        self.TP_style.layout("RoundedFrame",
                         [("RoundedFrame", {"sticky": "nsew"})])
        self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
        self.frame_one.pack()
        l1=Label(parent,image=self.borderImage).pack()
    def popup(self,x,y,width=110,height=120):
        self.width=width
        self.height=height
        self.app = Tk()
        self.app.config(bg=self.bg)
        self.app.geometry(f"{self.width}x{self.height}+{x}+{y}")
        self.app.wm_attributes("-topmost",True)
        self.app.overrideredirect(True)
        self.app.focus_force() 
        #self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
        #self.frame_one.pack()
        #l1=Label(self.app,image=self.borderImage).pack()
        #self.m.pack_propagate(0)
        def destroy(event):
            self.app.destroy()
        self.app.bind("<FocusOut>",destroy)
if __name__ == "__main__":
    root = Tk()
    menu = menu(root)
    def evt(evt):
        menu.popup(evt.x_root,evt.y_root,width=200,height=400)     
    root.bind("<Button-3>",evt)
    root.mainloop()
     

如果我像

一样为框架编写样式
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)

init() 方法中,一切正常。如果我在弹出方法中写同样的东西(我刚刚注释掉的地方),我得到以下错误。

Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
        return self.func(*args)
      File "C:\Users\mishra\P_Menu.py", line 64, in evt
        menu.popup(evt.x_root,evt.y_root,width=200,height=400)
      File "C:\Users\mishra\P_Menu.py", line 51, in popup
        self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
      File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 740, in __init__
        Widget.__init__(self, master, "ttk::frame", kw)
      File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 557, in __init__
        tkinter.Widget.__init__(self, master, widgetname, kw=kw)
      File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
        self.tk.call(
    _tkinter.TclError: Layout RoundedFrame not found

标签也一样

l1=Label(self.app,image=self.borderImage).pack()

init() 部分一切正常,但在弹出方法中出现以下错误。

Traceback (most recent call last):
  File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\mishra\P_Menu.py", line 64, in evt
    menu.popup(evt.x_root,evt.y_root,width=200,height=400)
  File "C:\Users\mishra\P_Menu.py", line 53, in popup
    l1=Label(self.app,image=self.borderImage).pack()
  File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist    

我想知道如何修复它,因为我想知道它背后的逻辑并在 popup() 方法中使用。 任何帮助都会得到帮助。 谢谢!

可能是因为 popup 创建了一个新的 Tk 实例作为 self.app,但是样式属于 root = Tk() 创建的第一个 Tk 实例。所以第二个 Tk 实例的 child 无法识别它。或许您可以尝试将 self.app = Tk() 更改为 self.app = Toplevel() 看看是否有效?