Bottom Page

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")

Quote
#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)
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter window and turtle window error 1885 3 151 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 1,252 Jun-15-2018, 10:42 AM
Last Post: thatguy14
  [Tkinter] No menu bar in window bde69 5 3,566 Nov-25-2017, 06:35 PM
Last Post: heiner55
  update a variable in parent window after closing its toplevel window gray 5 2,848 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)