Search code examples
pythontkinterpycharm

is there a way to put Tkinter menu to final project?


I am creating a game for my exam project and am struggling to bring together my code and Tkinter menu.

I have a project called 'Arcade Final' which shows all the games a user can play. I import these games into my final code and call them after the relevant button is pressed.

How do I import the Tkinter code without having it in a white box like this, but just display the menu at the top?

the current output


Solution

  • Probable reason for your problem is you are calling Tk() in the sub-files, which calls the white box which is known as root window.

    You can solve this by Returning a Frame containing all widget, and then add menu bar in the main file.

    Example:

    • For the file with games:
    # test.py
    from tkinter import Frame,Label
    def a(root):
        a=Frame(root)
        b=Label(a,text="Hello!")
        b.pack()
        a.pack()
        return a
    
    • For main file:
    import tkinter as tk
    a1=tk.Tk()
    mb=  Menubutton ( a1, text="condiments", relief=RAISED )
    mb.pack()
    mb.menu =  Menu ( mb, tearoff = 0 )
    mb["menu"] =  mb.menu
    
    mayoVar = IntVar()
    ketchVar = IntVar()
    
    mb.menu.add_checkbutton ( label="mayo",
                              variable=mayoVar )
    mb.menu.add_checkbutton ( label="ketchup",
                              variable=ketchVar )
    a(a1)
    a1.mainloop()