-1

The part that has the problem is that I ask the user for an input in one class, the I wish to use the value obtained from that entry in another class, However, this code always give me 0 no matter what I have entered. I think it is probably because that when another class is executed, the value stored in the entry of the previous class disappear. But I don't know how to go around it. Could any coding master help me a little bit here?

......

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        BackButton = tk.Button(self, text="Back",font=TNR24,command=lambda: controller.show_frame(MainPage))
        PrintButton = tk.Button(self, text="Print it", command=self.print_message)
        ExitButton = tk.Button(self,text="EXIT",command=exit_window)
        ProceedButton=tk.Button(self, text="Proceed", command=lambda: controller.show_frame(PageTwo))
        self.NumOfVertices= tk.IntVar()
        global VertexNumber
        VertexNumber=self.NumOfVertices.get()
        NumOfVerticesEntry=tk.Entry(self,textvariable=self.NumOfVertices)
        ProceedButton.pack()
        BackButton.pack()
        ExitButton.place(x=1240, y=670, width=40, height=30)
        PrintButton.pack()
        NumOfVerticesEntry.pack()

    def print_message(self):
        print self.NumOfVertices.get()


class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        lable=tk.Label(self,text=VertexNumber)

......

The code is quite long so I only took the part which I need help on. VertexNumber is the variable that I want to store and use in the class Pagetwo. But it always turns into 0 no matter what I entered. Is there any ways to store that variable permanently immediately after the user enters it?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Zicong Ma
  • 21
  • 1
  • 4

1 Answers1

1

You could make NumOfVertices a global variable, and call NumOfVertices.get() in PageTwo to get the current value of the IntVar.

NumOfVertices = tk.IntVar()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        ...
        NumOfVerticesEntry = tk.Entry(self, textvariable=NumOfVertices)

    def print_message(self):
        print NumOfVertices.get()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        VertexNumber = NumOfVertices.get()
        label = tk.Label(self, text=VertexNumber)

Alternatively, to avoid the global variable, you could make NumOfVertices an attribute of the PageOne instance.

Then when you instantiate PageTwo, also pass the instance of PageOne so that its NumOfVertices attribute can be looked-up.

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        ...
        self.NumOfVertices = tk.IntVar()
        NumOfVerticesEntry = tk.Entry(self, textvariable=self.NumOfVertices)

    def print_message(self):
        print self.NumOfVertices.get()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller, pageone):
        tk.Frame.__init__(self, parent)
        VertexNumber = pageone.NumOfVertices.get()
        label = tk.Label(self, text=VertexNumber)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • The second method cause an error that there are 4 arguments but only 3 are expected, Is there any ways to solve this? – Zicong Ma Jan 23 '16 at 18:58
  • I think I defined earlier "def __init__(self, *args, **kwargs):" in the base of the code, – Zicong Ma Jan 23 '16 at 19:13
  • When you instantiate `PageTwo`, be sure to pass it the instance of `PageOne`. – unutbu Jan 23 '16 at 20:56
  • I would recommend the second method. You could also pass the value of PageTwo.print_message as an argument if you wanted to avoid passing the whole object. – BenJ Jan 23 '16 at 21:00
  • This code was designed with a controller, specifically for communicating between classes. Instead of passing the pages to each other, the controller simply needs a) to be saved for each page, and b) modified so that it can return a handle to a given page. Then, getting the data is as simple as `self.controller.get_page("PageOne").NumOfVerticies.get()` (or something to that effect). – Bryan Oakley Jan 25 '16 at 23:27