title: How to switch between different tkinter canvases from a start-up page and return back to start-up page in sub-canvas
body: <p>I have created a <a href="https://i.stack.imgur.com/3HtLq.png" rel="nofollow noreferrer">start-up canvas</a>, which contains buttoms to shfit to two other sub-canvases. In addition, in those two sub-canvases, buttom to return to start-up canvas is created.  However, after I enter the <a href="https://i.stack.imgur.com/5fNYN.png" rel="nofollow noreferrer">sub-canvas</a>, I fail to return to start-up canvas. That is to say, when I click the buttom to return to the start-up canvas, it will create a start-up canvas beside the sub-canvas instead of closing the subcanvas and shifting to the start-up canvas. Is there any way to switch between canvases and return to the main canvas? Thank you!</p>
  <pre><code>import tkinter as tk
  from tkinter import ttk
  import re
  
  
  class SampleApp(tk.Tk):
      def __init__(self):
          tk.Tk.__init__(self)
          self._Canvas= None
          self.switch_Canvas(StartUpPage)
  
      def switch_Canvas(self, Canvas_class):
          new_Canvas = Canvas_class(self)
          if self._Canvas is not None:
                  self._Canvas.destroy()
          self._Canvas = new_Canvas
          self._Canvas.pack()
  
  class StartUpPage(tk.Canvas):
      def __init__(self, master, *args, **kwargs):
          tk.Canvas.__init__(self, master, *args, **kwargs)
          tk.Frame(self)
          tk.Label(self, text=&quot;Example&quot;).grid(column = 0, row = 0)
          tk.Button(self, text=&quot;Canvas1&quot;,
                command=lambda: master.switch_Canvas(PageOne)).grid(column = 0, row = 1)
          tk.Button(self, text=&quot;Canvas2&quot;,
                command=lambda: master.switch_Canvas(PageTwo)).grid(column = 0, row = 2)
  
  
  class PageOne(tk.Canvas, tk.Tk):
      def __init__(self, master, *args, **kwargs):
          root = tk.Canvas.__init__(self, *args, **kwargs)
          self.frame1  = tk.Frame(root, width=430)
          self.frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
          tk.Label(self.frame1, text=&quot;First Canvas&quot;).pack(side=&quot;top&quot;, fill=&quot;x&quot;, pady=5)
          tk.Button(self.frame1, text=&quot;Back to start-up page&quot;,
                command=lambda: master.switch_Canvas(StartUpPage)).pack()
  
  class PageTwo(tk.Canvas, tk.Tk):
      def __init__(self, master, *args, **kwargs):
          root = tk.Canvas.__init__(self, *args, **kwargs)
          self.frame2  = tk.Frame(root, width=430)
          self.frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
          tk.Label(self.frame2, text=&quot;Second Canvas&quot;).pack(side=&quot;top&quot;, fill=&quot;x&quot;, pady=5)
          tk.Button(self.frame2, text=&quot;Back to start-up page&quot;,
                command=lambda: master.switch_Canvas(StartUpPage)).pack()
  
  
  if __name__ == &quot;__main__&quot;:
      app = SampleApp()
      app.mainloop()
  </code></pre>