【问题标题】:KeyError: <class '__main__.Page2'> in Python TkinterKeyError:Python Tkinter 中的 <class '__main__.Page2'>
【发布时间】:2020-10-22 09:55:14
【问题描述】:

我确实知道还有一些其他问题与我的类似,但没有一个答案有效。我正在 tkinter 中构建一个应用程序,当我尝试使用 lambda : self.show_frame(frame) 方法时它不起作用,它说 KeyError: ma​​in.Page2'>

请注意,函数 init 是缩进的,它只是在 stackoverflow 上没有缩进。

class tkinterApp(Tk): 
def __init__(self, *args, **kwargs):  
    # __init__ function for class Tk 
    Tk.__init__(self, *args, **kwargs) 
 
    # creating a container 
    container = Frame(self)   
    container.pack(side = "top", fill = "both", expand = True)  

    container.grid_rowconfigure(0, weight = 1) 
    container.grid_columnconfigure(0, weight = 1) 

    

    # initializing frames to an empty array 
    self.frames = {}   

    # iterating through a tuple consisting 
    # of the different page layouts 
    for F in (StartPage, Page1): 

        frame = F(container, self) 

        # initializing frame of that object from 
        # startpage, page1, page2 respectively with  
        # for loop 
        self.frames[F] = frame  

        frame.grid(row = 0, column = 0, sticky ="nsew") 
    
    mainMenu = Menu(self)
    self.config(menu=mainMenu)

    location = Menu(mainMenu)
    mainMenu.add_cascade(label="Quick Location", menu=location)
    location.add_command(label="SignUp", command=lambda : self.show_frame(Page1))

    #the code I am having an issue with
    location.add_command(label="SignIn", command=lambda : self.show_frame(Page2))
    #the code I am having an issue with

    location.add_command(label="Homepage", command=lambda : self.show_frame(StartPage))

    location.add_command(label="Quit", command=self.quit)
    
    self.show_frame(StartPage) 

# to display the current frame passed as 
# parameter 
def show_frame(self, cont): 
    frame = self.frames[cont] 
    frame.tkraise() 

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    您忘记在for F in (...) 中包含Page2

    for F in (StartPage, Page1, Page2):
        ...
    

    【讨论】:

    • 非常感谢,我只是有时会想念那些小细节。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多