Tkinter throwing a KeyError when trying to change frames

2024/11/20 3:35:45

I'm learning tkinter off of the Sentdex tutorials and I into a problem when trying to change pages. My compiler throws something about a KeyError that it doesn't give whenever I change the button on the start page to change to itself rather than PageOne:

Exception in Tkinter callback
Traceback (most recent call last):File "C:\Users\Jason\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__return self.func(*args)File "C:/Users/Jason/PycharmProjects/gui/main.py", line 43, in <lambda>button1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(PageOne))File "C:/Users/Jason/PycharmProjects/gui/main.py", line 29, in show_frameframe = self.frames[cont]
KeyError: <class '__main__.PageOne'>

and my code:

import tkinter as tkLARGE_FONT=("Verdana", 12)class SeaofBTCapp(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] = frameframe.grid(row=0, column=0, sticky="nsew")self.show_frame(StartPage)def show_frame(self, cont):frame = self.frames[cont]frame.tkraise()def qf(param):print(param)class StartPage(tk.Frame):def __init__(self, parent, controller):tk.Frame.__init__(self, parent)label = tk.Label(self, text="Start Page", font=LARGE_FONT)label.pack(pady=10,padx=10)#command within button cant throw args to funcs. Use lambda to throw those args to the func insteadbutton1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(PageOne))button1.pack()class PageOne(tk.Frame):def __init__(self, parent, controller):tk.Frame.__init__(self, parent)label = tk.Label(self, text="Start Page", font=LARGE_FONT)label.pack(pady=10,padx=10)#command within button cant throw args to funcs. Use lambda to throw those args to the func insteadbutton1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(StartPage))button1.pack()app = SeaofBTCapp()
app.mainloop()
Answer

Your button is calling show_frame(PageOne), but you never created an instance of PageOne. So, of course, there is no key in self.frames that matches that page.

Perhaps you intended to create an instance of PageOne in your initial code?

def __init__(self, *args, **kwargs):...frame = PageOne(container, self)self.frames[PageOne] = frame...

If you don't want to create the page until you need it, you can add that code in show_frame. First, you'll need to make container an instance variable (ie: self.container), then modify show_frame to look something like this:

def show_frame(self, cont):if cont not in self.frames:self.frames[cont] = cont(self.container, self)frame = self.frames[cont]frame.tkraise()
https://en.xdnf.cn/q/118463.html

Related Q&A

How to send messages to other clients only in the sequence of adding clients?

https://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/sever.pyhttps://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/client.pyWhen server starts, and suppose three clients …

Dictionary keeps getting overwritten in each iteration of for-loop

import randomo=[,,!,@,#,$,%,^,&,*,(,),,_,=,+,/,[] q=[1,2,3,4,5,6,7,8,9,0]for i in top_25:wordDic ={i: random.choice(o)+random.choice(q)} print(wordDic)(top_25 is an array of words, and the random.c…

Python socket communication with HP print server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question appears to be off-topic because it lacks sufficient information to diagnose the proble…

Why are torch.version.cuda and deviceQuery reporting different versions?

I have a doubt about the CUDA version installed on my system and being effectively used by my software. I have done some research online but could not find a solution to my doubt. The issue which helpe…

How to have 2 advertisements in BLE(BlueTooth Low Energy)?

Im working on BLE advertisement. Id like to know if its possible to have 2 advertisements in BLE. I need to have both service data and manufacturer data. Im using Python. The code is based on https://g…

How to get Python script to write to existing sheet

I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:#import the writer import xlwt #import the reader impor…

Python3 - getting the sum of a particular row from all the files [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Selenium Headers in Python

how can I change my headers in Selenium like I do in requests ,I want to change my cookies and headers . from myCookie import cookie from selenium import webdriver from selenium.webdriver.common.by imp…

Get unique groups from a set of group

I am trying to find unique groups in a column(here for letter column) from an excel file. The data looks like this:id letter1 A, B, D, E, F3 B, C2 B75 T54 K, M9 D, B23 B, D, A34 X, Y, Z67 X, Y12 E, D15…

Move and Rename files using Python

I have .xls files in a directory that i need to move to another directory and renamed. Those files are updated monthly and will have a different name each month. For instance the current name is Geoc…