2

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 lot of research I am struggling to find an example explaining how to do it.

I tried to create a minimal example following several posts but it isn't working yet and I don't really understand what I am doing to be honest.

My biggest reference is this post here but it's quite long and complex: tkinter - Going Back and Forth Between Frames Using Buttons

This example here on the other hand isn't quite enough for me to get it. Both combined and explained might do the trick already: Tkinter open and close Toplevel windows

  • Can you help me create a minimal example (with or without the code for the actual window)?
  • And explain what is going on. I assume it's simple so explanations can be fairly short I guess. (If you know of a tutorial site explaining it well I would be happy as well)

Thanks in advance, hopefully we can create a helpful resource for me and others.

 def Forward(self):    
        # Open secondary Window
        Secondary_Win = Toplevel()  
        #Close primary Window
        Main_Win.withdraw() #.deiconify() to show again
        Main_Win.destroy()

 def Backward(self):    
        # Close secondary Window
        Secondary_Win.withdraw 
        #Open primary Window
        Main_Win.deiconify()

EDIT: as @stovfl pointed out to clarify: I want to switch between actual Windows not just framees. Sorry for the wording hiccup.

G.M
  • 345
  • 3
  • 22
  • You have to clarify if you want to switch between `tk.Frame`s inside **one** window, which is usually the main window or switch between multiple `tk.Toplevel` windows. – stovfl Aug 30 '19 at 07:49

1 Answers1

2

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

from tkinter import *


root = Tk()



class temp_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()


    def Forward(self):    
        # Open secondary Window
        if not 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()



    def Backward(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.

temp123
  • 362
  • 1
  • 4
  • Seems pretty much perfect to me, thanks. the `def __init__` naming scheme is just common practice I assume? – G.M Aug 30 '19 at 08:58
  • Thank you, yes that's correct: The def __init__ is the constructor of the class and is used to initialize the class variables. – temp123 Aug 30 '19 at 09:05
  • Thank you, that is a good point! I've now updated my answer, please see my edit. – temp123 Sep 03 '19 at 11:37
  • @temp123: OK, one pedantic point: This `if not self.secondary_win:` should read `if self.secondary_win is None:` – stovfl Sep 04 '19 at 07:10