0

When I press a button, I want to open a new side. Not a new window, the window should be the same, just the interface should change.

How can I solve this without opening a new window?

from tkinter import *

page1=Tk()
label1=Label(page1, text="This is page 1")
label1.pack()

def topage2():
    page2=Tk()
    label2=Label(page2, text="This is page 2")
    label2.pack()

button=Button(page1, text="To page 2", command=topage2)
button.pack()

mainloop()
user2963623
  • 2,267
  • 1
  • 14
  • 25
Duardo
  • 107
  • 11

1 Answers1

0

You could create two frames in the same place, and lifting them over one another using the lift and lower methods (example taken from Bryan Oakley here and slightly altered):

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.frame = tk.Frame(self)
        self.frame2 = tk.Frame(self)
        self.frame.place(relwidth=1, relheight=0.8, relx=0, rely=0)
        self.frame2.place(relwidth=1, relheight=0.8, relx=0, rely=0)
        self.label = tk.Label(self.frame, text="Hello, world")
        button1 = tk.Button(self, text="Click to hide label",
                           command=self.hide_label)
        button2 = tk.Button(self, text="Click to show label",
                            command=self.show_label)
        self.label.pack()
        button1.place(relwidth=0.5, relheight=0.15, relx=0.0, rely=0.825)
        button2.place(relwidth=0.5, relheight=0.15, relx=0.5, rely=0.825)

    def show_label(self, event=None):
        self.frame.lift(self.frame2)

    def hide_label(self, event=None):
        self.frame.lower(self.frame2)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

You could place 'page one' in one frame and 'page two' in the other

Community
  • 1
  • 1
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • Is there another way? – Duardo Jun 30 '14 at 18:01
  • You could 'undo' the placement of the widgets all together using `pack_forget` and `grid_forget` (explained [here](http://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible)), but i doubt that would be the preferred way to go. What exactly is wrong with using multiple stacked frames? – fhdrsdg Jun 30 '14 at 20:03