How to pass data from one page to another page in python tkinter?
Question: my program is this.. import tkinter as tk from tkinter import *

    
    
    TITLE_FONT = ("Helvetica", 18, "bold")
    class SampleApp(tk.Tk):
        def __init__(self, *args, **kwargs):
    
            tk.Tk.__init__(self, *args, **kwargs)
            container = tk.Frame(self)
            self.title(" Allocation")
            width, height = self.winfo_screenwidth(), self.winfo_screenheight()
            self.geometry('%dx%d+0+0' % (width,height))
            self.state('zoomed')
            self.wm_iconbitmap('icon.ico')
            container.pack(side="top", fill="both", expand=True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
    
            self.frames = {}
            for F in (StartPage, PageOne, PageTwo):
                frame = F(container, self)
                self.frames[F] = frame
                frame.grid(row=0, column=0, sticky="nsew")
    
            self.show_frame(StartPage)
    
        def show_frame(self, c):
            frame = self.frames[c]
            frame.tkraise()
    
    class StartPage(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            logo = tk.PhotoImage(file="backesh.ppm")
            BGlabel = tk.Label(self,image=logo)
            BGlabel.image = logo
            BGlabel.place(x=0,y=0,width=592,height=450)
            label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
            label.place(x=0,y=0,width=592,height=44)
            frame1 = Frame(self)
            Label(frame1, bd=5,bg="black",text="  Enter text :  ",font=("Helvetica", 14),fg="light green",width=21).pack(side=LEFT)
            emp=Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
            emp.pack(side=LEFT)
            frame1.place(x=400,y=160)
    
            button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame(PageOne))
            button2 = tk.Button(self, text="Go to Page two",
                            command=lambda: controller.show_frame(PageTwo))
            button3 = tk.Button(self, text="Exit",
                            command=self.quit)
            button1.place(x=100,y=406,width=200,height=44)
            button2.place(x=300,y=406,width=200,height=44)
            button3.place(x=500,y=406,width=80,height=44)
    
    
    class PageOne(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            logo = tk.PhotoImage(file="backesh.ppm")
            BGlabel = tk.Label(self,image=logo)
            BGlabel.image = logo
            BGlabel.place(x=0,y=0,width=592,height=450)
            label = tk.Label(self, text="This is page one", font=TITLE_FONT)
            label.place(x=0,y=0,width=592,height=44)
    
            button1 = tk.Button(self, text="Go to Start Page",
                            command=lambda: controller.show_frame(StartPage))
        #button2 = tk.Button(self, text="Go to Page two",
         #                   command=lambda: controller.show_frame(PageTwo))
            button3 = tk.Button(self, text="Exit",
                            command=self.quit)
            button1.place(x=100,y=406,width=200,height=44)
            button3.place(x=300,y=406,width=200,height=44)
    
    class PageTwo(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            logo = tk.PhotoImage(file="backesh.ppm")
            BGlabel = tk.Label(self,image=logo)
            BGlabel.image = logo
            BGlabel.place(x=0,y=0,width=592,height=450)
            label = tk.Label(self, text="This is page two", font=TITLE_FONT)
            label.place(x=0,y=0,width=592,height=44)
    
            button1 = tk.Button(self, text="Go to Start Page",
                            command=lambda: controller.show_frame(StartPage))
        #button2 = tk.Button(self, text="Go to Page two",
         #                   command=lambda: controller.show_frame(PageTwo))
            button3 = tk.Button(self, text="Exit",
                            command=self.quit)
            button1.place(x=100,y=406,width=200,height=44)
            button3.place(x=300,y=406,width=200,height=44)
    
    if __name__ == "__main__":
        app = SampleApp()
        app.mainloop()
    

i want to take entry text data from StartPage and display it as label in
PageOne. How to do it? I am new to this. Please type the code. Thanks in
advance.


Answer: Firstly, correct the code of the class **PageOne** and **StartPage** and add
_self.controller = controller_ to the __init__ function:

    
    
    class PageOne(tk.Frame):
        def __init__(self, parent, controller):
            #your code
            self.controler = controller
    

Add self before entry field in StartPage and before label in PageOne:

    
    
    #entry in StartPage
    self.emp=tk.Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
    self.emp.pack(side=tk.LEFT)
    
    #label in PageOne
    self.label = tk.Label(self, text="This is page one", font=TITLE_FONT)
    self.label.place(x=0,y=0,width=592,height=44)
    

Then add a function go_to_page_one to StartPage class:

    
    
    def go_to_page_one(self):
        self.controller.SomeVar = self.emp.get() #save text from entry to some var
        self.controller.frames[PageOne].correct_label() #call correct_label function
        self.controller.show_frame(PageOne) #show page one
    

On button1 in StartPage class change command to lambda: self.go_to_page_one():

    
    
    button1 = tk.Button(self, text="Go to Page One",
                        command=lambda: self.go_to_page_one())
    

At last add a function correct label to the class PageOne:

    
    
    def correct_label(self):
        self.label.config(text=self.controller.SomeVar) #correct the label
    

