0

I have built my first few scripts with a nice little GUI on them, as the tutorials have shown me, but none of them address what to do for a little more complex program. I want to print the values entered in InputPage in second_page.when I try to run my program every frame is getting ready even before entering my input.Here I tried to print the first entry in second_page frame by using a variable named "alpha" .How can I deal with this?

from tkinter import *
from tkinter import ttk

LARGE_FRONT=("Verdana",12) 

class simmulator(Tk):

 def __init__(self, *args, **kwargs):
  Tk.__init__(self, *args, **kwargs)

  container = 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 (InputPage,second_page):
      frame = F(container, self)
      self.frames[F] = frame
      frame.grid(row=0, column=0, sticky="nsew")
  self.show_frame(InputPage)

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

 def get_page(self, page_class):
  return self.frames[page_class]


class InputPage(Frame):

 def __init__(self, parent, controller):
  self.controller = controller
  Frame.__init__(self,parent)        
  label = Label(self, text="simmulator",font=LARGE_FRONT)
  label.grid(row=0, column=0, sticky ='n', columnspan =2)
  # i brought your variable in the class for example sake 
  namesInput = ["Na:", "Nd:", "Temp(t):"]
  self.entryWidgets = [] # we want to call this in another function so we assign it as self.variableName

  labelWidgets = []
  self.alpha = None

        #LOOP TO CREATE WIDGETS
  for i in range(0, len(namesInput)):
   labelWidgets.append(Label(self, text = namesInput[i]))
   self.entryWidgets.append(Entry(self))
   labelWidgets[-1].grid(row= i+1, column =0, sticky='e')
   self.entryWidgets[-1].grid(row= i+1, column = 1, sticky='w')

  submit = ttk.Button(self, text = "Submit", command = lambda:[self.push_retrieve_solutions(),controller.show_frame(second_page)])
  submit.grid(row = 6, column =0, columnspan =2)

 def getEntries(self):
  results = []
  for x in self.entryWidgets: # i.e for each widget in entryWidget list
   results.append(x.get())
  self.alpha = results[0]
  return results

 def push_retrieve_solutions(self):
  print(self.getEntries())

class second_page(Frame):
  def __init__(self, parent, controller):
   self.controller = controller
   Frame.__init__(self,parent)     

   label = Label(self, text="Parameter",font=LARGE_FRONT)
   label.grid(row=0, column=0, sticky ='n', columnspan =2)

   ctrler = self.controller.get_page(InputPage)

   label1 = Label(self,text = "Na  =")
   label1.grid(row= 1, column =1, sticky='e')
   fabel1 = Label(self, text = ctrler.alpha)
   fabel1.grid(row= 1, column = 2, sticky='w')


app = simmulator()
app.mainloop()
  • Change `self.alpha` to a `StringVar` and associate it to the first entry in `InputPage` and the label `fabel1` of `second_page` via `textvariable` option. Also remove the line `self.alpha = results[0]` as well. – acw1668 May 06 '20 at 05:18
  • Thanks @acw1668 brother! – Sandeep Vulluri May 06 '20 at 09:34
  • I recommend looking at all of the answers linked to in this answer: https://stackoverflow.com/a/7557028/7432 – Bryan Oakley May 06 '20 at 14:48

0 Answers0