Python Forum
[Tkinter] Command button, then more command buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Command button, then more command buttons
#1
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()
Reply
#2
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.
Reply
#3
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.
Reply
#4
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()
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Continue command in python tkinter? MLGpotato 7 354 Mar-27-2021, 04:59 AM
Last Post: deanhystad
  [PyGUI] My GUI crashes after command MLGpotato 1 180 Feb-21-2021, 03:17 PM
Last Post: deanhystad
  Get name of command button Heyjoe 3 366 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  guizero repeat command pdihawk 2 554 Sep-28-2020, 01:32 PM
Last Post: pdihawk
  ui to py command error Diovanno 4 11,903 Aug-26-2020, 07:12 AM
Last Post: zorekk
  Class function does not create command button Heyjoe 2 480 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Creating a frame with 4 command buttons Heyjoe 5 549 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  [Tkinter] button command tkinter Heyjoe 6 1,638 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 615 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  Unable pass the proper row number to Menubutton command kenwatts275 2 409 Jul-19-2020, 11:46 AM
Last Post: kenwatts275

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020