Home > Software engineering >  I want it to detect when I close the other menu and reopen this tkinter menu.(python)
I want it to detect when I close the other menu and reopen this tkinter menu.(python)

Time:10-13

This is the code I am using, if there are suggestions, I will gladly accept them. I want root to re-open after I close lvls_menu.menu(). I've tried a loop import but it didn't work, soo pls enlighten by 2 brain cells.

root = Tk()
root.geometry("700x600")
root.title("*The Stargazers* (MAIN MENU)")
icon = PhotoImage(file = "C:\\Users\\LENOVO\\Desktop\\Adeeb\\pythonProjects\\ALT project\\LOGO.png")
root.iconphoto(False, icon)


# background for the menu cause it needs to look nice
background = PhotoImage(file = "C:\\Users\\LENOVO\\Desktop\\Adeeb\\pythonProjects\\ALT project\\bg1.png")

# canvas so it looks pretty
canvas1 = Canvas(root, width=700, height=600, cursor='plus')
canvas1.place(anchor='nw')

# lets draw our background
canvas1.create_image( 0, 0, image = background, anchor = "nw")


# All the prompts
main_menu = canvas1.create_text(100, 30, text="MAIN MENU", font=("Arial", 20, "bold", "underline"),
                                fill="purple")
start = canvas1.create_text(60, 88, text = "GO! :", font=("Verdana", 20, "bold"), fill="purple")
htp = canvas1.create_text(60, 138, text = "How to ", font=("Verdana", 20, "bold"), fill="purple")
htp2 = canvas1.create_text(60, 160, text = "play :", font=("Verdana", 20, "bold"), fill="purple")

# All the functions


def click():
    root.destroy()
    lvls_menu.menu()


# All the buttons
start_button = Button(root, text="START", command = lambda:click(), padx=20, pady=20, fg="purple",
                      bg= "white", font=("bold", 20), cursor="plus")
start_button.place(x=110, y=65, width=100, height=45)

controlls_button = Button(root, text="CONTROLLS", command = controls.controlls_menu, padx=20, pady=20,
                          fg="purple",bg= "white", font=("Arial", 15), cursor="plus")
controlls_button.place(x=120, y=130, width=125, height=50)

credits_button = Button(root, text="CREDITS", command = lambda:controls.credits_menu(), padx=20, pady=20,
                          fg="purple",bg= "white", font=("Arial", 15), cursor="plus")
credits_button.place(x=576, y=550, width=125, height=50)

root.mainloop() 

CodePudding user response:

When you try to do such a thing in Tkinter it can be quite messy, I would suggest trying to move your menu() from lvls_menu into your main python script, and wrapping your whole main menu in another function so you can just reference the functions without having to go through messing with imports.

I have edited the provided code to this rough version that you can hopefully integrate into your program.

If you truly wanted to completely separate them, then your best bet would be to use pyinstaller to create exe's and use os.startfile() to start each executable that was made from your scripts, and just use exit() instead of .destroy() if you choose to go down that route since as I said importing modules for different menus can get extremely messy in Tkinter I would also suggest creating classes for menus and putting all the menu's respective functions inside of that class to somewhat organize it.

from tkinter import *
global root, menuRoot



def main_window():
    global root
    root = Tk()
    root.geometry("700x600")
    root.title("*The Stargazers* (MAIN MENU)")

    icon = PhotoImage(file="C:\\Users\\LENOVO\\Desktop\\Adeeb\\pythonProjects\\ALT project\\LOGO.png")
    root.iconphoto(False, icon)

    background = PhotoImage(file = "C:\\Users\\LENOVO\\Desktop\\Adeeb\\pythonProjects\\ALT project\\bg1.png")
    canvas1 = Canvas(root, width=700, height=600, cursor='plus')
    canvas1.place(anchor='nw')

    canvas1.create_image( 0, 0, image = background, anchor = "nw")


    # You don't need to assign variables when adding items to the canvas
    canvas1.create_text(100, 30, text="MAIN MENU", font=("Arial", 20, "bold", "underline"),
                                    fill="purple")
    canvas1.create_text(60, 88, text = "GO! :", font=("Verdana", 20, "bold"), fill="purple")
    canvas1.create_text(60, 138, text = "How to ", font=("Verdana", 20, "bold"), fill="purple")
    canvas1.create_text(60, 160, text = "play :", font=("Verdana", 20, "bold"), fill="purple")

    start_button = Button(root, text="START", command=lambda: click(), padx=20, pady=20, fg="purple",
                          bg="white", font=("bold", 20), cursor="plus")
    start_button.place(x=110, y=65, width=100, height=45)

    controlls_button = Button(root, text="CONTROLLS", command=controls.controlls_menu, padx=20, pady=20,
                              fg="purple", bg="white", font=("Arial", 15), cursor="plus")
    controlls_button.place(x=120, y=130, width=125, height=50)

    credits_button = Button(root, text="CREDITS", command=lambda: controls.credits_menu(), padx=20, pady=20,
                            fg="purple", bg="white", font=("Arial", 15), cursor="plus")
    credits_button.place(x=576, y=550, width=125, height=50)

def menu():
    global menuRoot
    # This is just a place holder chunk of code, add all your functionality from lvls_menu.menu() to this, and add all dependices from lvls_menu to this main script.
    menuRoot = Tk()
    menuRoot.geometry("500x500")
    menuCanvas = Canvas(width=500,height=500)
    menuCanvas.pack(fill="both", expand=True)

    mainMenu = Button(text="Main Menu", command=go_back)
    menuCanvas.create_window(150, 150, anchor="nw", window=mainMenu)



def go_back():
    global menuRoot
    menuRoot.destroy()
    main_window()

def click():
    global root
    root.destroy()
    menu()


if __name__ == '__main__':
    main_window()
root.mainloop()
  • Related