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
  I'm trying to create a GUI in Python which would accept a bash command max22 1 661 Nov-13-2023, 04:40 AM
Last Post: deanhystad
  [PyGUI] Invalid command error with Entry object eliqm 8 2,027 May-18-2023, 10:14 PM
Last Post: eliqm
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,776 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [PyQt] command require close window Krissstian 14 2,826 Nov-19-2022, 04:18 PM
Last Post: Krissstian
  [Tkinter] Scrollable buttons with an add/delete button Clich3 5 3,332 Jun-16-2022, 07:19 PM
Last Post: rob101
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 5,691 Nov-11-2021, 08:45 PM
Last Post: menator01
  [Tkinter] Extracting Data from a Command Subroutine gw1500se 0 1,088 Nov-11-2021, 08:02 PM
Last Post: gw1500se
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,274 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  Continue command in python tkinter? MLGpotato 7 8,337 Mar-27-2021, 04:59 AM
Last Post: deanhystad
  [PyGUI] My GUI crashes after command MLGpotato 1 1,855 Feb-21-2021, 03:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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