Im trying to get this menu to be inside my of my canvas but so far ive only had 2 windows open and now all im getting is an error, any help is greatly appreciated

Im using Tkinter as my GUI

from Tkinter import *

def startMenu():
    top_tk = Tk()
    top_tk.title("Checkers")
    top = Canvas(width="800", height="800")

    ## Creates the black and white square background
    size = 100
    cols = 8
    rows = 8
    squareList = []
    for i in range(cols):
        for j in range(rows):
            if j%2: colours = ["black", "white"]
            else: colours = ["white", "black"]
            squareList.append(top.create_rectangle(i*size,  j*size,
                                                   i*size+size, j*size+size,
                                                   fill=colours[i%2]))

    ## Creates the Checkers Heading
    frame1 = Frame(top, relief=GROOVE, borderwidth=2)
    label1 = Label(frame1, text='Checkers', font=('Arial', 48), fg="blue")
    label1.pack(side="left")

    top.create_window(540, 300, window=frame1, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Start Button
    frame2 = Frame(top, relief=GROOVE, borderwidth=2)
    button1 = Button(frame2, text='Start', font=('Arial', 28), fg="blue")
    button1.pack(side="left")

    top.create_window(457, 450, window=frame2, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Option Button
    frame3 = Frame(top, relief=GROOVE, borderwidth=2)
    button2 = Button(frame3, text='Options', font=('Arial', 28), fg="blue")
    button2.pack(side="left")

    top.create_window(480, 550, window=frame3, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Help Button
    frame4 = Frame(top, relief=GROOVE, borderwidth=2)
    button3 = Button(frame4, text='Help', font=('Arial', 28), fg="blue")
    button3.pack(side="left")

    top.create_window(458, 650, window=frame4, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Exit Button
    frame5 = Frame(top, relief=GROOVE, borderwidth=2)
    button4 = Button(frame5, text='Quit',command=quit, font=('Arial', 28), fg="blue")
    button4.pack(side="left")

    top.create_window(455, 750, window=frame5, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Not working
    menubar = Menu(top)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="New")
    filemenu.add_command(label="Open")
    filemenu.add_command(label="Save")
    filemenu.add_command(label="Save as...")
    filemenu.add_command(label="Close")
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=top.quit)
    menubar.add_cascade(label="File", menu=filemenu)
    top.config(menu=menubar)

    ## Runs the mainloop
    top.mainloop()
    
## Runs the program
startMenu()

Recommended Answers

All 11 Replies

Hi.

Note for next time: GIVE MORE INFO!

You were having an error with the top.config() command. This is backed up by the fact that I took the not working code and changed it to this:

from Tkinter import *

root = Tk()

menubar = Menu(root)
frame = Frame()
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=frame.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)


root.mainloop()

it worked fine. However, this is not a top level menu.

I've had a look round but I can't see what's wrong. Now we've indentified the problem I hope that somebody can shed some light upon it...

You cannot place a menubar inside a canvas. A menubar goes on the top level window by design. You can place Tkinter widgets on a canvas by using a canvas window object.

Im trying to create a top level window, but im still trying to get it to work. After alot of trial and error i have got the following code

from Tkinter import *

def startMenu():
    top = Tk()
    top.title("Checkers")
    top = Canvas(width="800", height="800")

    top.option_add('*tearOff', FALSE)
    win = Toplevel(top)
    menubar = Menu(win)
    win['menu'] = menubar
    menubar = Menu(top)
    menu_file = Menu(menubar)
    menu_edit = Menu(menubar)
    menubar.add_cascade(menu=menu_file, label='File')
    menubar.add_cascade(menu=menu_edit, label='Edit')


    ## Creates the black and white square background
    size = 100
    cols = 8
    rows = 8
    squareList = []
    for i in range(cols):
        for j in range(rows):
            if j%2: colours = ["black", "white"]
            else: colours = ["white", "black"]
            squareList.append(top.create_rectangle(i*size,  j*size,
                                                   i*size+size, j*size+size,
                                                   fill=colours[i%2]))

    ## Creates the Checkers Heading
    frame1 = Frame(top, relief=GROOVE, borderwidth=2)
    label1 = Label(frame1, text='Checkers', font=('Arial', 48), fg="blue")
    label1.pack(side="left")

    top.create_window(540, 300, window=frame1, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Start Button
    frame2 = Frame(top, relief=GROOVE, borderwidth=2)
    button1 = Button(frame2, text='Start', font=('Arial', 28), fg="blue")
    button1.pack(side="left")

    top.create_window(457, 450, window=frame2, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Option Button
    frame3 = Frame(top, relief=GROOVE, borderwidth=2)
    button2 = Button(frame3, text='Options', font=('Arial', 28), fg="blue")
    button2.pack(side="left")

    top.create_window(480, 550, window=frame3, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Help Button
    frame4 = Frame(top, relief=GROOVE, borderwidth=2)
    button3 = Button(frame4, text='Help', font=('Arial', 28), fg="blue")
    button3.pack(side="left")

    top.create_window(458, 650, window=frame4, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Exit Button
    frame5 = Frame(top, relief=GROOVE, borderwidth=2)
    button4 = Button(frame5, text='Quit',command=quit, font=('Arial', 28), fg="blue")
    button4.pack(side="left")

    top.create_window(455, 750, window=frame5, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Runs the mainloop
    top.mainloop()


startMenu()

But unfortunatly it still does not work

i have just decided to try and use a cascading menu instead of a toplevel menu, im getting an error i know nothing about:

TypeError: 'classobj' object does not support item assignment

here is my code

from Tkinter import *

def startMenu():
    top = Tk()
    top.title("Checkers")
    top = Canvas(width="800", height="800")

    ## Creates the black and white square background
    size = 100
    cols = 8
    rows = 8
    squareList = []
    for i in range(cols):
        for j in range(rows):
            if j%2: colours = ["black", "white"]
            else: colours = ["white", "black"]
            squareList.append(top.create_rectangle(i*size,  j*size,
                                                   i*size+size, j*size+size,
                                                   fill=colours[i%2]))

    ## Creates the Checkers Heading
    frame1 = Frame(top, relief=GROOVE, borderwidth=2)
    label1 = Label(frame1, text='Checkers', font=('Arial', 48), fg="blue")
    label1.pack(side="left")

    top.create_window(540, 300, window=frame1, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Start Button
    frame2 = Frame(top, relief=GROOVE, borderwidth=2)
    button1 = Button(frame2, text='Start', font=('Arial', 28), fg="blue")
    button1.pack(side="left")

    top.create_window(457, 450, window=frame2, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Option Button
    frame3 = Frame(top, relief=GROOVE, borderwidth=2)
    button2 = Button(frame3, text='Options', font=('Arial', 28), fg="blue")
    button2.pack(side="left")

    top.create_window(480, 550, window=frame3, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Help Button
    frame4 = Frame(top, relief=GROOVE, borderwidth=2)
    button3 = Button(frame4, text='Help', font=('Arial', 28), fg="blue")
    button3.pack(side="left")

    top.create_window(458, 650, window=frame4, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the Exit Button
    frame5 = Frame(top, relief=GROOVE, borderwidth=2)
    button4 = Button(frame5, text='Quit',command=quit, font=('Arial', 28), fg="blue")
    button4.pack(side="left")

    top.create_window(455, 750, window=frame5, anchor=SE)
    top.pack(fill='both', expand='yes')

    ## Creates the File button up the top
    frame6 = Frame(top, relief=GROOVE, borderwidth=2)
    button5 = Button(frame6, text='File', font=('Arial', 10), fg="red")
    button5.pack(side="left")

    top.create_window(43, 35, window=frame6, anchor=SE)
    top.pack(fill='both', expand='yes')

    frame6 = Frame(top, relief=GROOVE, borderwidth=2)
    button5 = Button(frame6, text='File', font=('Arial', 10), fg="red")
    button5.pack(side="left")

    top.create_window(43, 35, window=frame6, anchor=SE)
    top.pack(fill='both', expand='yes')
###########################################################
    menuframe = Frame(top, relief=GROOVE, borderwidth=2)
    mb = Menubutton
    mb.menu = Menu()
    mb['menu'] = mb.menu
    mb.Menu.add_cascade(label="text")
    top.create_window(300,300, window=menuframe, anchor=SE)
    top.pack(fill='both', expand='yes')
    

    ## Runs the mainloop
    top.mainloop()


startMenu()

all help is appriciated

You are assigning the name top to the top level window and the Canvas. Change the name of one of them. I messed with part of your code until it worked.

menuframe = Frame(top, relief=GROOVE, borderwidth=2)
    mb = Menubutton(top)
    mb.menu = Menu(mb)
    mb['menu'] = mb.menu
    mb.menu.add_cascade(label="text")
    top_canvas.create_window(300,300, window=menuframe, anchor=SE)

I tried you code but i am now getting

'top_canvas' is not defined.

its probobly a just a spelling error

:)

I said "PROBOBLY"

I tried you code but i am now getting

'top_canvas' is not defined.

Look back through your code. You assigned the name "top" to your canvas. Your top level window is also named "top". To get it to work, I changed the name of the canvas to "top_canvas". Gees, show some effort.

Look back through your code. You assigned the name "top" to your canvas. Your top level window is also named "top". To get it to work, I changed the name of the canvas to "top_canvas". Gees, show some effort.

Ok, i am putting alot of effort into this i just dont have a clue what you mean by change one of them, and i dont even have a top level menu all i have is buttons and labels, i just used the word top because thats what the examples had.

Ok, i am putting alot of effort into this i just dont have a clue what you mean by change one of them, and i dont even have a top level menu all i have is buttons and labels, i just used the word top because thats what the examples had.

def startMenu():
    top = Tk()
    top.title("Checkers")
    [B]top_canvas[/B] = Canvas(width="800", height="800")

"top" is the top level window, is it not? I renamed the canvas "top_canvas". You had the canvas named "top" also which prevented you from adding a menubar to the top level window.

You may want to do some serious editing. Rename 'top' to 'root' and call the canvas 'canvas' or something like that. I don't know how your trying to create a cascade menu, but it obviously doesn't work. Check out my tutorial:

http://www.youtube.com/watch?v=AxFOIaFYJq4

There is an annotated link to the extension on the video.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.