samuel west
samuel west

Reputation: 23

how to make a button lead to another GUI

So I have a program that has the main menu and the desired output I want to have is when a button is pressed on the main menu the next menu is revealed. so for example this is the code for a section of the main menu:

import tkinter as tk
HEIGHT = 700
WIDTH = 800

root = tk.Tk()

canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
canvas.pack()

frame = tk.Frame(root, bg = '#8BDFF4')
frame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)


button1 = tk.Button(frame, text = "view table", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
button1.place(relx = 0, rely = 0, relwidth = 0.25,relheight = 0.25)
root.mainloop()

so my problem is getting the button labeled 'view table' when pressed to open this second menu and close the main menu at the same time:

import tkinter as tk
HEIGHT = 700
WIDTH = 800

root = tk.Tk()

canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
canvas.pack()

frame = tk.Frame(root, bg = '#8BDFF4')
frame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)


button1 = tk.Button(frame, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
button1.place(relx = 0, rely = 0.35, relwidth = 0.25,relheight = 0.25)
root.mainloop()

I think maybe it could be done with classes however I'm not very experienced with using tkinter so if any of you could give some guidance it would be great, Thanks :)

Upvotes: 2

Views: 416

Answers (1)

Sister Coder
Sister Coder

Reputation: 324

You can create a function which destroys the frame and creates the main menu beneath it.

import tkinter as tk
HEIGHT = 700
WIDTH = 800

root = tk.Tk()

canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
canvas.pack()

frame = tk.Frame(root, bg = '#8BDFF4')
frame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)
#Function to destroy the frame
def change():
    frame.destroy()
    HEIGHT = 700
    WIDTH = 800

    canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
    canvas.pack()
    #Renamed the second frame
    framee = tk.Frame(root, bg = '#8BDFF4')
    framee.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)

    button1 = tk.Button(framee, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
    button1.place(relx = 0, rely = 0.35, relwidth = 0.25,relheight = 0.25)

#command=change, tells the button what function should run when it's clicked
button1 = tk.Button(frame, text = "view table", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A',command=change)
button1.place(relx = 0, rely = 0, relwidth = 0.25,relheight = 0.25)
root.mainloop()

Upvotes: 1

Related Questions