Skip to content Skip to sidebar Skip to footer

Go Back And Forth Between Tkinter Windows

I am currently struggling to create a tkinter Project which includes several Windows. All I want is to go forward regularly through my program and backwards step by step. After a l

Solution 1:

Perhaps this is somewhat close to what you're looking for:

from tkinter import *


root = Tk()



classtemp_frame:

    def__init__(self, master):
        self.master = master
        self.secondary_win = None
        self.btn_next = Button(self.master, text="Forward", command=self.Forward)
        self.btn_next.pack()


    defForward(self):    
        # Open secondary Windowifnot self.secondary_win:
            self.secondary_win = Toplevel()
            back_btn = Button(self.secondary_win, text="Back", command=self.Backward)
            back_btn.pack()
            self.master.withdraw()
        else:
            self.secondary_win.deiconify()
            self.master.withdraw()



    defBackward(self):    
        self.secondary_win.withdraw()
        self.master.deiconify()


temp = temp_frame(root)

root.mainloop()

Explanation:

A frame is created with the help of the class temp_frame. The frame holds the functions for backwards and forwards, opening a new window when forward is pressed and withdrawing the new window when backwards is pressed. When the new window is withdrawn, the main window is brought forward.

Edit: Revised code to avoid creating a new window everytime "Forward" was pressed.

Post a Comment for "Go Back And Forth Between Tkinter Windows"