1

I want to get the text in the Entry field while passing from Page1 to Page2 and I want to pass it as a parameter to Page2 :

Here is my code

import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        self.frames ={}
        for f in  (Page1, Page2):
            frame = f(container,self)
            self.frames[f] = frame
            frame.grid(row = 1000, column = 500, sticky = "nsew")
        self.show_frame(Page1)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class Page1(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        Frame0 = tk.Frame(self)
        Frame0.pack()
        Frame1 = tk.Frame(self)
        Frame1.pack()
        tk.Label(Frame0,text = "Page 1").pack()
        v = tk.StringVar()
        def say_hello():
            print(v.get())
        e = tk.Entry(Frame0, textvariable = v).pack()
        tk.Button(Frame1,text = "Page 2", command = (lambda: controller.show_frame(Page2))and say_hello).pack()

class Page2(tk.Frame) :
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        Frame0 = tk.Frame(self)
        Frame0.pack()
        Frame1 = tk.Frame(self)
        Frame1.pack()
        tk.Label(Frame0, text="Page 2").pack()
        tk.Button(Frame1,text = "Page 1", command = (lambda: controller.show_frame(Page1))).pack()

app = MyApp()
app.mainloop()

How to make this please?

(sorry for my bad English)

j_4321
  • 15,431
  • 3
  • 34
  • 61
paulWich
  • 19
  • 1
  • 1
    What do you mean by "pass it as a parameter"? In this example, `Page2` is created before the user is given a chance to enter any data. What function are you wanting to call with the parameter? Does the following answer your question? http://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter-python-3 – Bryan Oakley May 15 '17 at 11:35
  • I want to create a button in Page2 with reference to the checkbox i have created in Page1 , is that possible ? – paulWich May 15 '17 at 12:47

1 Answers1

1

If you want the Page1 Entrybox content to be also available in Page2, your best option is to define and store the corresponding StringVar in Page1 and Page2's common controller instance:

class MyApp(tk.Tk):
    def __init__(self):
        ...
        # Store the StringVar in MyApp's instance
        self.v = tk.StringVar()
        ...

Then you can access it as controller.v, in Page1:

class Page1(tk.Frame):
    def __init__(self,parent,controller):
        ...
        tk.Entry(Frame0, textvariable = controller.v).pack()
        tk.Button(Frame1,text = "Page 2", 
                  command=lambda: controller.show_frame(Page2)).pack()

and Page2 as well:

class Page2(tk.Frame) :
    def __init__(self,parent,controller):
        ...
        tk.Button(Frame0, text="Print Entry Value", 
                  command=lambda: print(controller.v.get())).pack()
Josselin
  • 2,593
  • 2
  • 22
  • 35
  • Can I, with this structure of code, create a button (for exemple) in Page2 with reference to the checkbox I have created in Page1 ? – paulWich May 15 '17 at 12:48
  • yes, actually, in my answer, the button in the `Page2` class does just that :) – Josselin May 15 '17 at 13:06