-2

I am trying to develop a Tkinter GUI which contains two pages, the first one to input stock name (counter_selection) and the second one to plot stock price data. However, when I tried to use the data input from the first class using controller.get_page function it does not return the value input by Entry. The code is as below:

class Application(Tk.Tk):
    '''A GUI Application for FCN Demo'''

    def __init__(self):
        '''Initialization of frame'''
        Tk.Tk.__init__(self)
        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 = {}
        for F in (StartPage, counter_selection, plot_counter):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row= 0, column = 0, sticky = "nsew")

        self.show_frame(StartPage)

    def get_page(self,classname):
        '''Returns an instace of page given it's class name as a string'''
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None

    def show_frame(self,cont):

        frame = self.frames[cont]
        frame.tkraise()



class counter_selection(Tk.Frame):

    def __init__(self, parent, controller):
        '''This is to create the widgets to input stock'''
        Tk.Frame.__init__(self,parent)
        self.controller = controller
        label1 = Tk.Label(self, text = "Please Enter 3 counter names")
        label1.pack(padx = 10, pady = 10)

        self.entry1 = Tk.Entry(self)
        self.entry1.pack()

        self.entry2 = Tk.Entry(self)
        self.entry2.pack()

        self.entry3 = Tk.Entry(self)
        self.entry3.pack()

        button1 = Tk.Button (self, text = 'Confirm', command = lambda: controller.show_frame(plot_counter))

        button1.pack()

class plot_counter(Tk.Frame):
    '''This is to plot the graph of three selected counters'''
    def __init__(self,parent,controller):
         Tk.Frame.__init__(self,parent)
         self.controller = controller
         counterPage = self.controller.get_page('counter_selection')
         self.counter1 = counterPage.entry1.get()
         self.counter2 = counterPage.entry2.get()
         self.counter3 = counterPage.entry3.get()
         label1 = Tk.Label(self, text = self.counter11)
         label1.pack()

The label1 does not show anything on the Frame, suggesting that it seems failed to get the value from the class. What's my mistake?

(PS: I didn't put the StartPage in since it is irrelevant for the question)

martineau
  • 119,623
  • 25
  • 170
  • 301
Kevin Ling
  • 540
  • 2
  • 7
  • 15

1 Answers1

0

Why is Tkinter Entry's get function returning nothing?

TL;DR: your call to entry1.get() is done only once while instancing the plot_counter object, and never called again. Try putting it in a function to be called when displaying the Frame.

WKnight02
  • 182
  • 1
  • 11