1

I'm kind of a newbie to Python, and I'm writing some code to take data via a user input and put it into a .csv file. To do that, the program needs to pass data from class to class.

To teach myself how to pass data, I took code from here. I did have to alter the code a bit to get it to start up, making sure that the make_widget and print_it functions can pull the "name" variable stored in self.app_data data structure properly.

from tkinter import *
from tkinter import ttk
class MyApp(Tk):

def __init__(self):
    Tk.__init__(self)
    self.app_data={'name': StringVar}
    container = ttk.Frame(self)
    container.pack(side="top", fill="both", expand = True)
    self.frames = {}
    for F in (PageOne, PageTwo):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky = NSEW)
    self.show_frame(PageOne)

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

class PageOne(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller=controller
        ttk.Label(self, text='PageOne').grid(padx=(20,20), pady=(20,20))
        self.make_widget(controller)

    def make_widget(self, controller):
        self.controller=controller
        self.some_entry = ttk.Entry(self, textvariable=self.controller.app_data['name'], width=8) 
        self.some_entry.grid()
        button1 = ttk.Button(self, text='Next Page',command=lambda: controller.show_frame(PageTwo))
        button1.grid()

class PageTwo(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller=controller
        ttk.Label(self, text='PageTwo').grid(padx=(20,20), pady=(20,20))
        button1 = ttk.Button(self, text='Previous Page',command=lambda: controller.show_frame(PageOne))
        button1.grid()
        button2 = ttk.Button(self, text='press to print', command= self.print_it())
        button2.grid()

    def print_it(self):
        value=self.controller.app_data['name'].get()
        print ('The value stored in StartPage some_entry = ', value)#What do I put here 
        #to print the value of some_input from PageOne   

When I run this program, it does start up, and I can move from frame to frame, but it does not print the "name" variable.

When I close the window, I get the error:

TypeError: get() missing 1 required positional argument: 'self'

Which the traceback blames on the line:

value=self.controller.app_data['name'].get()

What am I doing wrong? For what it's worth, I'm writing the code in Python 3.5.

I really appreciate any help that you guys could give me.

Community
  • 1
  • 1
CJG
  • 11
  • 3
  • i think you are missing a parenthesis for the vars before .get() Ex. (.app_data['name']).get() or (self.controller.app_data['name]_.get() depending on what you are trying to achieve – glls May 12 '16 at 16:29
  • additionally, why do you have this? self.app_data={'name': StringVar} and not just self.app_name = Stringvar() ? – glls May 12 '16 at 16:34
  • button2's command will be triggered as soon as you run the program. Remove the paranthesis. `button2 = ttk.Button(...., command = self.print_it)` – Lafexlos May 12 '16 at 17:12
  • I think I just figured out what I did wrong. – CJG May 12 '16 at 17:30
  • 1
    I think I just figured out what I did wrong. I was trying to implement the changes Bryan Oakley suggested to Paul D's code, and got a little distracted by error messages (i.e. getting "TypeError: 'dict' object is not callable" when I used parentheses instead of brackets). Again, I'm a n00b. Thank you all for the replies! – CJG May 12 '16 at 17:48
  • Yeah @CJG , scoping issues were my biggest problem when first learning tkinter, you'll get the hang of things dont worry. – Gunner Stone May 12 '16 at 17:56

0 Answers0