Python Forum

Full Version: Command button, then more command buttons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am a beginner trying to learn Tkinter.

The objectives of the following code are to
1) Show a red "Press here to start button."
2) After the "Press here to start button is pressed it disappears and 5 option command buttons come up.

Questions
1) The red "Press here to start button" does not disappear, when the option buttons appear. I know I could use pack_forget or destroy to accomplish this but I can't get them to work.
2) Is using a function to get the second set of command buttons to come up, the usual way this is done or is there a better way?


from tkinter import *
root = Tk()
root.geometry("800x800")

#Secondmenu brings up a second set of buttons.  How do I make the "Press here to starbutton" disappear before this second set of buttons come up

def secondmenu():

    Option1  = Button(root, text="Option1", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",).pack()

    Option2  = Button(root, text="Option2", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",).pack()

    Option3  = Button(root, text="Option3", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30", ).pack()

    Option4  = Button(root, text="Option4", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30", ).pack()

    Option5  = Button(root, text="Option5", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",).pack()


#This is the button you will see when you start the program

start = Button(root, text="Press here to start", bg="red", fg="black", font = "Helvitica 30",command = secondmenu).pack()
#start.pack_forget()


root.mainloop()
The commented out #start.pack_forget() deosn't work because when creating the button .pack() has been called which returns None.
to keep a reference to the button change
start = Button(root, text="Press here to start", bg="red", fg="black", font = "Helvitica 30",command = secondmenu).pack()
to
start = Button(root, text="Press here to start", bg="red", fg="black", font = "Helvitica 30",command = secondmenu)
and then call start.pack() on the next line

start.pack_forget() will now work but you don't want that happening straight after packing the button as it won't appear.
You only want this to happen after the button is pressed so move this to the start of the function secondmenu

The code will now look like
import tkinter as tk
root = tk.Tk()
root.geometry("800x800")

# Secondmenu brings up a second set of buttons.  How do I make the "Press here to starbutton" disappear before this second set of buttons come up


def secondmenu():
    start.pack_forget()

    Option1 = tk.Button(root, text="Option1", bg="white",
                        fg="firebrick", relief="groove", font="Helvitica 30",).pack()

    Option2 = tk.Button(root, text="Option2", bg="white",
                        fg="firebrick", relief="groove", font="Helvitica 30",).pack()

    Option3 = tk.Button(root, text="Option3", bg="white",
                        fg="firebrick", relief="groove", font="Helvitica 30", ).pack()

    Option4 = tk.Button(root, text="Option4", bg="white",
                        fg="firebrick", relief="groove", font="Helvitica 30", ).pack()

    Option5 = tk.Button(root, text="Option5", bg="white",
                        fg="firebrick", relief="groove", font="Helvitica 30",).pack()


# This is the button you will see when you start the program

start = tk.Button(root, text="Press here to start", bg="red",
                  fg="black", font="Helvitica 30", command=secondmenu)
start.pack()


root.mainloop()
Note I removed from tkinter import * because Namespace flooding with * imports

Is this is the best way to do it?, depends on how your GUI will function.
Thanks I got it to work.

A least one website I viewed since reading your answer indicate that wildcard imports are usually considered bad practice.
Dear Python Users:

I've added some submenus to the option buttons.

1) First start the program.
2) Press the red "Press here to start button"
5 option buttons will come up.
3) Press option1. 3 finalsel buttons come up (but the options buttons don't disappear).

In the "final function" there are 5 commented out commands at the beginning of the function.
I am a beginner but I am sure these commands don't work because of namespace and/or scope issues.

Is there a way to get the option buttons to disappear without using global variables?

import tkinter as tk
root = tk.Tk()
root.geometry("800x800")


#Secondmenu brings up a second set of buttons.  How do I make the "Press here to starbutton" disappear before this second set of buttons come up

def secondmenu():
    start.pack_forget()

    Option1  = tk.Button(root, text="Option1", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",command = final).pack()

    Option2  = tk.Button(root, text="Option2", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",command = final).pack()

    Option3  = tk.Button(root, text="Option3", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",command = final).pack()

    Option4  = tk.Button(root, text="Option4", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",command = final).pack()

    Option5  = tk.Button(root, text="Option5", bg="white", \
                            fg="firebrick", relief = "groove", font = "Helvitica 30",command = final).pack()

def final ():
    
    #Option1.pack_forget()
    #Option2.pack_forget()
    #Option3.pack_forget()
    #Option4.pack_forget()
    #Option5.pack_forget()
    finalsel1= tk.Button(root, text="finalsel1", bg="white", \
                        fg="firebrick", relief="groove", font="Helvitica 30", ).pack()

    finalsel2 = tk.Button(root, text="finalsel2", bg="white", \
                        fg="firebrick", relief="groove", font="Helvitica 30", ).pack()

    finalsel3 = tk.Button(root, text="finalsel3", bg="white", \
                        fg="firebrick", relief="groove", font="Helvitica 30", ).pack()


#This is the button you will see when you start the program

start = tk.Button(root, text="Press here to start", bg="red", fg="black", font = "Helvitica 30",command = secondmenu)

start.pack()

root.mainloop()
Using Classes to make frames that contain buttons, destroying a frame will also destroy any child widgets of the frame.
import tkinter as tk


class Main:
    def __init__(self, root):
        self.root = root
        self.root.geometry("800x800")
        self.create_start_frame()

    def create_start_frame(self):
        self.start_frame = StartFrame(self.root)
        self.start_frame.btn_start.bind(
            '<Button-1>', self.on_start_frame_btn_start)
        self.start_frame.pack()

    def on_start_frame_btn_start(self, event):
        self.start_frame.destroy()
        self.second_menu_frame = SecondMenuFrame(root)
        self.second_menu_frame.btn_option1.bind(
            '<Button-1>', self.on_second_menu_frame_btn_option1)
        self.second_menu_frame.pack()

    def on_second_menu_frame_btn_option1(self, event):
        self.second_menu_frame.destroy()
        self.final_frame = FinalFrame(root)
        self.final_frame.pack()


class StartFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='StartFrame').pack()
        self.btn_start = tk.Button(
            self, text="Press here to start", bg="red", fg="black", font="Helvitica 30")
        self.btn_start.pack()


class SecondMenuFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='SecondMenuFrame').pack()
        self.btn_option1 = tk.Button(self, text="Option1", bg="white",
                                     fg="firebrick", relief="groove", font="Helvitica 30")
        self.btn_option1.pack()


class FinalFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='FinalFrame').pack()
        self.btn_final1 = tk.Button(self, text="finalsel1", bg="white",
                                    fg="firebrick", relief="groove", font="Helvitica 30", )
        self.btn_final1.pack()


if __name__ == "__main__":
    root = tk.Tk()

    main = Main(root)
    root.mainloop()