亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我得到鑰匙錯誤:<類__main__。在試圖舉起另一幀時?

我得到鑰匙錯誤:<類__main__。在試圖舉起另一幀時?

喵喔喔 2022-09-27 10:08:20
我收到此錯誤 -KeyError: <class '__main__.PublishPage'>這是我的代碼 -p = 0#Now, Adding the different pages for the gameclass HeadlineGame(tk.Tk):    def __init__(self, *args, **kwargs):        tk.Tk.__init__(self, *args, **kwargs)        container = tk.Frame(self)        container.pack(side="top", fill="both", expand=True)        container.grid_rowconfigure(0, weight=1)        container.grid_columnconfigure(0, weight=1)        self.frames = {}        frame = StartPage(container, self)        self.frames[StartPage] = frame        frame.grid(row=0, column=0, sticky="nsew")        self.show_frame(StartPage)    def show_frame(self, controller):        frame = self.frames[controller]        frame.tkraise()class StartPage(tk.Frame):    def __init__(self, parent, controller):        tk.Frame.__init__(self, parent)        publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage))        publishbutton.image = publishimg        publishbutton.place(x=503, y=315)class PublishPage(tk.Frame):        def __init__(self, parent, controller):         button2 = tk.Button(self, text="Test")         button2.grid(row=0, column=0)app = HeadlineGame()app.geometry('1366x768')app.mainloop()我試圖解決這個問題,但無濟于事。這是 lambda 命令的問題嗎?我想要它,以便當我按下按鈕時,它會將我帶到第二幀,即使這個特定問題無法解決,有沒有辦法實現這一點?謝謝。編輯回溯錯誤:> Exception in Tkinter callback Traceback (most recent call last):   File> "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py",> line 1883, in __call__>     return self.func(*args)   File "C:\Users\DELL\Desktop\Python\intro.py", line 276, in <lambda>>     publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage))   File> "C:\Users\DELL\Desktop\Python\intro.py", line 197, in show_frame>     frame = self.frames[controller] KeyError: <class '__main__.PublishPage'>
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

我建議重構這樣的東西。 現在將負責根據請求實例化新幀;你不必管理自己。show_frame()self.frames


class HeadlineGame(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        self.container = container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)


        container.grid_rowconfigure(0, weight=1)

        container.grid_columnconfigure(0, weight=1)


        self.frames = {}

        self.show_frame(StartPage)


    def show_frame(self, controller):

        if controller not in self.frames:

            self.frames[controller] = frame = controller(self.container, self)

            frame.grid(row=0, column=0, sticky="nsew")

        frame = self.frames[controller]

        frame.tkraise()



class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        publishbutton = tk.Button(

            self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)

        )

        publishbutton.image = publishimg

        publishbutton.place(x=503, y=315)



class PublishPage(tk.Frame):

    def __init__(self, parent, controller):

        button2 = tk.Button(self, text="Test")

        button2.grid(row=0, column=0)



app = HeadlineGame()

app.geometry("1366x768")

app.mainloop()


查看完整回答
反對 回復 2022-09-27
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號