Tkinter move between frame on button click

Question:

I have a GUI in tkinter, and I want to understand how to switch from a frame to another. I mean, if I click a button on my GUI I want to switch to another frame that show the result returned from action related to that button, and in the same frame I want a button "return home" to switch new to the home frame. I attach the code.

if __name__ == '__main__':
    [...]
    client = Client(url)
    w = Fullscreen_Window()    

    bottone_visualizza_ricetta_attuale = tk.Button(text="Visualizza ricetta attuale", command= lambda: visualizza_ricetta_attuale(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_visualizza_ricetta_attuale.grid(row=0, column=1, sticky="W")

    bottone_visualizza_lista_ricette = tk.Button(text="Visualizza lista ricette", command= lambda: visualizza_lista_ricette(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_visualizza_lista_ricette.grid(row=1, column=1, sticky="W")

    bottone_avvio_selezione = tk.Button(text="Avvia la selezione", command= lambda: avvio_selezione(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_avvio_selezione.grid(row=2, column=1, sticky="W")

    bottone_stop_selezione = tk.Button(text="Termina la selezione", command= lambda: stop_selezione(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_stop_selezione.grid(row=3, column=1, sticky="W")


    bottone_cambia_velocita = tk.Button(text="Cambia velocità sezione o vibratore", activebackground="YELLOW",height=5,bd=4,width=30)# command=#TODO)
    bottone_cambia_velocita.grid(row=4, column=1, sticky="W")

    
    bottone_arresta_vibratori_sezione = tk.Button(text="Arresta i vibratori di una sezione", activebackground="YELLOW",height=5,bd=4,width=30)# command=#TODO)
    bottone_cambia_velocita.grid(row=5, column=1, sticky="W")

    bottone_avvio_svuota_residuo = tk.Button(text="Avvia svuotamento residuo", command= lambda:svuota_residuo(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_avvio_svuota_residuo.grid(row=6, column=1, sticky="W")

    bottone_stop_svuota_residuo = tk.Button(text="Termina svuotamento residuo", command= lambda:fine_svuotamento_residuo(client), activebackground="YELLOW",height=5,bd=4,width=30)
    bottone_stop_svuota_residuo.grid(row=7, column=1, sticky="W")

    bottone_seleziona_ricetta = tk.Button(text="Seleziona una ricetta", activebackground="YELLOW",height=5,bd=4,width=30)# command=#TODO)
    bottone_seleziona_ricetta.grid(row=8, column=1, sticky="W")
    
    w.tk.mainloop()

def visualizza_ricetta_attuale(client):
    return "eseguita la funzione visualizza ricetta attuale"

[..]
Asked By: Giuliano Stirparo

||

Answers:

You need to delete (or hide) a frame and show another.
Example:

import tkinter as tk
root = tk.Tk()

def switchframe():
   frame1.pack_forget()
   frame2.pack(fill=tk.BOTH,expand=True)

frame1 = tk.Frame(root)
frame1.pack(fill=tk.BOTH,expand=True)
btn1 = tk.Button(frame1, text="cliccami", command=switchframe)
frame2 = tk.Frame(root)
btn2 = tk.Button(frame2, text="sono il secondo bottone")

obviously you pass your parameters to the function and in the function you create all your widgets.

Answered By: JacopoBiondi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.