Tkinter Bugs wrt adding mulitple buttons to menu bar

Attempting to create a menu box but I am having trouble adding multiple buttons, there is only a "Quit" button visible when I run the code, please assist xx

class Menu(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)

        self.master = master 

        self.init_menu()

    def init_menu(self):
        self.master.title("DUNGEON MENU")

        self.pack(expand = True, fill=BOTH)
        quitB = Button(self, text = "Quit", fg = "red", command = self.client_exit)
        quitB.grid(row = 0, column = 3, padx = 120)
    def client_exit(self):
        exit()


        lootB = Button(self, text = "Loot", command = None)
        lootB.grid(row = 1, column = 3, padx = 120)


root = Tk()
root.geometry("300x300")
app = Menu(root)
root.mainloop()
button.pack()```

Upvotes: 0

Views: 24

Answers (1)

milanbalazs
milanbalazs

Reputation: 5329

I have created a working version from your code. You can find my finding in the below code as comments.

I am not totally sure what your expectation about your code. Now the "Quit" and "Loot" buttons are visible on the Frame and if you click to "Quit" button, the program will be end (Loot button does nothing).

Code:

from tkinter import Frame, BOTH, Button, Tk


class Menu(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)

        self.master = master

        self.init_menu()
        # You have to call the "client_exit" method to rendering the "Loot" button to Frame.
        self.client_exit()

    def init_menu(self):
        self.master.title("DUNGEON MENU")

        self.pack(expand=True, fill=BOTH)
        # Destroy the master if you click to the "Quit" button.
        quitB = Button(self, text="Quit", fg="red", command=lambda: self.master.destroy())
        quitB.grid(row=0, column=0, padx=120)

    def client_exit(self):
        # exit() # This exit is not needed because it breaks the program running.
        # You can define the call-back of button in "command" parameter of Button object.
        lootB = Button(self, text="Loot", command=None)
        lootB.grid(row=1, column=0, padx=120)


root = Tk()
root.geometry("300x300")
app = Menu(root)
root.mainloop()
# button.pack() # It does not have effect. You cannot define widgets after "mainloop"

Output:

>>> python3 test.py 

enter image description here

Upvotes: 1

Related Questions