Python Forum
Menu bar not showing in gui window - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Menu bar not showing in gui window (/thread-12722.html)



Menu bar not showing in gui window - mlh - Sep-09-2018

Hello all,

I am needing to learn tkinter to complete a a gui project. I would like to be able to separate the components that I need to code into their own classes and files. The job that I have, is to create the menu bar for an application. For testing purposes, I have created an Application class and would like to be able to call my MenuBar class from within the Application class. Everything works, except when I run the application, the gui window does not show any menus. Python is not my "native" language, so I am having to learn Python on top of learning tkinter. I am running Python on Windows 10. Also, this is my first post in this forum, so please excuse me for any shortcomings.

Here are my files:

Application.py
import tkinter
from menuBar import MenuBar

class Application:
	def __init__(self):
		# Create and display a test window for viewing the menus
		window = tkinter.Tk()
		window.minsize(500, 500)

		# Create an instance of the MenuBar class and display it to the window
		mb = MenuBar()
		window.config(menu = mb)

		window.mainloop()

Application()
menuBar.py
import tkinter

class MenuBar:
	def __init__(self):
		# Create and display the main menu bar
		menuBar = tkinter.Menu()

		# Create a pull-down menu for file operations
		fileMenu = tkinter.Menu(master = None, tearoff = False)
		menuBar.add_cascade(menu = fileMenu, label = "File")
		fileMenu.add_command(label = "New")
		fileMenu.add_command(label = "Open...")
		fileMenu.add_command(label = "Close")
		fileMenu.add_command(label = "Exit")
		
		# Create a pull-down menu for editing operations
		editMenu = tkinter.Menu(master=None, tearoff = False)
		menuBar.add_cascade(menu = editMenu, label = "Edit")
		editMenu.add_command(label = "Cut")
		editMenu.add_command(label = "Copy")
		editMenu.add_command(label = "Paste")
		editMenu.add_command(label = "Select All")
		editMenu.add_command(label = "Undo")
		editMenu.add_command(label = "Redo")

		# Create a pull-down menu for help operations
		helpMenu = tkinter.Menu(master = None, tearoff = False)
		menuBar.add_cascade(menu = helpMenu, label = "Help")
		helpMenu.add_command(label = "About")



RE: Menu bar not showing in gui window - Larz60+ - Sep-09-2018

You needed to provide window info to menu class:

Application.py
import tkinter
from menuBar import MenuBar
 
class Application:
    def __init__(self):
        # Create and display a test window for viewing the menus
        window = tkinter.Tk()
        window.minsize(500, 500)
 
        # Create an instance of the MenuBar class and display it to the window
        mb = MenuBar(window)
        # window.config(menu = mb)
 
        window.mainloop()

if __name__ == '__main__': 
    Application()
menuBar.py
import tkinter
 
class MenuBar:
    def __init__(self, window):
        # Create and display the main menu bar
        menuBar = tkinter.Menu(window)
 
        # Create a pull-down menu for file operations
        fileMenu = tkinter.Menu(menuBar, tearoff = False)
        fileMenu.add_command(label = "New")
        fileMenu.add_command(label = "Open...")
        fileMenu.add_command(label = "Close")
        fileMenu.add_command(label = "Exit")
        menuBar.add_cascade(menu = fileMenu, label = "File")
         
        # Create a pull-down menu for editing operations
        editMenu = tkinter.Menu(menuBar, tearoff = False)
        editMenu.add_command(label = "Cut")
        editMenu.add_command(label = "Copy")
        editMenu.add_command(label = "Paste")
        editMenu.add_command(label = "Select All")
        editMenu.add_command(label = "Undo")
        editMenu.add_command(label = "Redo")
        menuBar.add_cascade(menu = editMenu, label = "Edit")
 
        # Create a pull-down menu for help operations
        helpMenu = tkinter.Menu(menuBar, tearoff = False)
        helpMenu.add_command(label = "About")
        menuBar.add_cascade(menu = helpMenu, label = "Help")
        window.config(menu=menuBar)



RE: Menu bar not showing in gui window - mlh - Sep-09-2018

Thank you for your response and time Larz60+. I have mainly coded in the C family of languages, so GUI Python programming is something that is very foreign to me. Thanks again.