Python Forum
Menu bar not showing in gui window
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Menu bar not showing in gui window
#1
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")
Reply
#2
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)
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] QWidgetAction of QFrame not showing in menu malonn 4 1,942 Sep-22-2022, 10:49 PM
Last Post: malonn
  [PyQt] Matplotlib figure not showing cursor coordinates in the figure window mart79 0 2,052 Mar-26-2020, 01:48 PM
Last Post: mart79
  tkinter window and turtle window error 1885 3 6,624 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 4,590 Jun-15-2018, 10:42 AM
Last Post: thatguy14
  [Tkinter] No menu bar in window bde69 5 10,777 Nov-25-2017, 06:35 PM
Last Post: heiner55
  update a variable in parent window after closing its toplevel window gray 5 8,976 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020