-1

I want to have a two page GUI. Page one will have the day-2-day content and page 2 will be all the setup variables. My code creates two pages and defaults to Page 1. When I click on the Page 2 button, the page 2 data is shown, but the Page 1 data doesn't go away. After that, no matter which button is selected, content for both pages is displayed. I want only one page content to be displayed at a time with the ability to change pages as required.

import tkinter as tk
from tkinter import *
from tkinter import ttk

root=tk.Tk()
root.geometry("1000x500")
root.title ('Multi-Page GUI')
frame=tk.Frame(root,bg='lightblue')
frame.place(relx=0.1,rely=0.1,relheight=0.85,relwidth=0.85)

def page1():
    bt1.lower(frame)
    bt.lift(frame)
    labelpage1 = Label(root,text ="Page One", bg='lightblue', font=("Arial",12)).place(x=100,y=100)
                             
def page2():
    bt.lower(frame)
    bt1.lift(frame)
    labelpage1 = Label(root,text ="Page Two", bg='lightblue', font=("Arial",12)).place(x=100,y=200)

bt=tk.Button(root,text='Page 1',command=page1)
bt.grid(column=0,row=0)

bt1=tk.Button(root,text='Page 2',command=page2)
bt1.grid(row=0,column=1)

command = page1()

root.mainloop() 

Result after both buttons clicked

1 Answers1

0

I'm not sure its possible to lift frames without using Object Orientated Programming, but one way to do it is delete page2 when you want show page1, then delete page 2 when you want to show page1.

root = tk.Tk()
root.geometry("1000x500")
root.title('Multi-Page GUI')
frame = tk.Frame(root, bg='lightblue')
frame.place(relx=0.1, rely=0.1, relheight=0.85, relwidth=0.85)

frame2 = tk.Frame(root, bg='lightblue')
frame2.place(relx=0.1, rely=0.1, relheight=0.85, relwidth=0.85)


def page1():
    frame2.place_forget()
    frame.place(relx=0.1, rely=0.1, relheight=0.85, relwidth=0.85)
    Label(frame, text="Page One", bg='lightblue', font=("Arial", 12)).place(x=100, y=100)


def page2():
    frame.place_forget()
    frame2.place(relx=0.1, rely=0.1, relheight=0.85, relwidth=0.85)
    Label(frame2, text="Page Two", bg='lightblue', font=("Arial", 12)).place(x=100, y=200)


bt = tk.Button(root, text='Page 1', command= page1)
bt.grid(column=0, row=0)

bt1 = tk.Button(root, text='Page 2', command= page2)
bt1.grid(row=0, column=1)

command = page1()

root.mainloop()

See Using buttons in Tkinter to navigate to different pages of the application? for multiple pages in tkinter

coderoftheday
  • 1,987
  • 4
  • 7
  • 21