Python Tkinter menu bars don't display

后端 未结 4 1774
孤街浪徒
孤街浪徒 2021-01-06 05:12

I\'m trying to make a GUI using Tkinter and have come to implementing a menu bar. I\'ve looked at a few tutorials and written some code for it, but a menu bar never seems to

4条回答
  •  粉色の甜心
    2021-01-06 05:44

    Code with Explanation

    From personal experience, I have found that it is usually easier to manage all widgets in a widgets method. That is what I did here, and it worked. Also, instead of parent, I used master. I will now walk you through the code step-by-step.

    from Tkinter import *
    

    We import Tkinter (GUI stuff)

    class App(Frame):
    

    We create a class called App, which is the Frame where widgets are held.

        def __init__(self, master):
            Frame.__init__(self, master)
            self.grid()
            self.widgets()
    

    We create a method called __init__. This initializes the class, and runs another method called widgets.

        def widgets(self):
    
    
            menubar = Menu(root)
            menubar.add_command(label="File")
            menubar.add_command(label="Quit", command=root.quit())
    
            root.config(menu=menubar)
    

    We create the widgets method. This is where the widget, menubar is added. If we were to create anymore widgets, they would also be here.

    root=Tk()
    root.title("Menubar")
    app=App(root)
    root.mainloop()
    

    Lastly, we give the entire window some properties. We give it a title, Menubar, and run the App class. lastly, we start the GUI's mainloop with root.mainloop.

提交回复
热议问题