Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

tkinter, lambda Button with more than one command

I see that there are several posts with how to combine more than one command to a lambda Button, but none of them answer my question.

I want to combine two things: saving the input and changing to the next page. (def hello() is just to give me a quick way to see if it works or not). Now I can't figure out how to make my code save the file and also switch to the next site. Does anyone know how to do this?

Here is the part of my code where I am trying to do this:

def GetData(self):
userdata = self.ent1.get()

file = open("user1.txt", "w")
file.write(str(self.userdata))

file.close()


 def hello():
print("hello")


def combined(self, controller):
self.GetData()
self.hello()
controller.show_frame(StartPage)

class App(Tk):
def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)
    #Setup Menu
    MainMenu(self)
    #Setup Frame
    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 (StartPage, PageOne, PageTwo):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")



    self.show_frame(StartPage)

    self.geometry('400x500+500+50')

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



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

    label = Label(self, text="Start Page")
    label.pack(padx=10, pady=10)
    page_one = Button(self, text="Page One", command=lambda: 
controller.combined(self))
    page_one.pack()
    page_two = Button(self, text="Page Two", 
command=lambda:controller.show_frame(PageTwo))
    page_two.pack()
    ent1 = Entry(self)
    ent1.pack()
    l1 = Label(self, text="Firstname")
    l1.pack()


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

    label = Label(self, text="Page One")
    label.pack(padx=10, pady=10)
    start_page = Button(self, text="Start Page", 
command=lambda:controller.show_frame(StartPage))
    start_page.pack()
    page_two = Button(self, text="Page Two", 
command=lambda:controller.show_frame(PageTwo))
    page_two.pack()
    l1 = Label(self, text="Firstname")
    l1.pack()
    ent1 = Entry(self)
    ent1.pack()


app = App()
app.mainloop()

Errors I get is:

**line 62, in <lambda>
    page_one = Button(self, text="Page One", command=lambda: controller.combined(self))** 



 **line 2101, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'combined'**

Comments