Hello all, i was just wondering how you can make a button link to another page, for instance i want the help option on my start menu to link to a help page, if you can understand this can you please supply a small snippet of code, here is my code if it will be of any use.

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

startMenu()

Recommended Answers

All 6 Replies

This code runs. You overwrote your root and you were missing mainloop in the end.

from Tkinter import *

root = Tk()
root.title("Checkers")
top = Canvas(root,width="800", height="800") ## !!! Was overwriting the root window
## 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')

root.mainloop() ## !!!! Was missing

Here added to previous beginnings for single window menu system (media center style).

from Tkinter import *


def create_main():
    ## 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')
    main=frame1
    ## Creates the Start Button
    frame2 = Frame(top, relief=GROOVE, borderwidth=2)
    button1 = Button(frame2, text='Start', command=go_game, 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', command=go_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', command=go_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')

    return top

def create_help():
    frame = Frame(None, relief=GROOVE, borderwidth=2)
    ## Creates the Help Heading
    label1 = Label(frame, text='Help', font=('Arial', 48), fg="blue")
    label1.pack(side="left")
    
  ## Creates the Return Button
    button4 = Button(frame, text='Return',command=go_main, font=('Arial', 28), fg="blue")
    button4.pack(side="left")
    return frame

def create_options():
    frame = Frame(None, relief=GROOVE, borderwidth=2)
    ## Creates the Options Heading
    label1 = Label(frame, text='Options', font=('Arial', 48), fg="blue")
    label1.pack(side="left")
  ## Creates the Return Button
    button4 = Button(frame, text='Return',command=go_main, font=('Arial', 28), fg="blue")
    button4.pack(side="left")
    return  frame

def create_game():
    frame = Frame(None, relief=GROOVE, borderwidth=2)
    ## Creates the Game Heading
    label1 = Label(frame, text='Game', font=('Arial', 48), fg="blue")
    label1.pack()
  ## Creates the Return Button
    button4 = Button(frame, text='Return',command=go_main, font=('Arial', 28), fg="blue")
    button4.pack()
    return  frame

def go_game():
    global currentframe
    currentframe.pack_forget()
    currentframe=gameframe
    currentframe.pack()
    pass

def go_options():
    global currentframe
    currentframe.pack_forget()
    currentframe=optionsframe
    currentframe.pack()

def go_help():
    global currentframe
    currentframe.pack_forget()
    currentframe=helpframe
    currentframe.pack()

def go_main():
    global currentframe
    currentframe.pack_forget()
    currentframe=mainframe
    currentframe.pack()    
    
root = Tk()
root.title("Checkers")
top = Canvas(root,width="800", height="800") ## !!! Was overwriting the root window
## 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]))
currentframe = mainframe = create_main()
helpframe = create_help()
optionsframe = create_options()
gameframe = create_game()

go_main() ## unnecessary, but looks nice

root.mainloop() ## !!!! Was missing
commented: Amazing, helped me so much with my work +1

wow thank you so much for the help, it will help me further my project massivly

wow thank you so much for the help, it will help me further my project massivly

Thought out still my version of alternating colors.

Change main routine to this.

def diag_stripe(top,colors,size,cols,rows=None):
    if not rows: rows=cols ## can not put rows=cols in parameter list
    cl=len(colors)
    col=0
    squareList = []

    ## diag stripes for any size, any number of colors, rectangle
    for j in range(0,rows*size,size):
        for i in range (0,cols*size,size):
            fill=colors[col]
            squareList.append(top.create_rectangle(i,j,i+size, j+size,fill=fill))
            col=(col+1)%cl
        ## make to start with different color next line,
        ## if number of colors divides the number of cols
        if not cols % cl: col=(col+1)%cl 
    
root = Tk()
root.title("Checkers")
top = Canvas(root,width="800", height="800") ## !!! Was overwriting the root window

## Creates the alternating color square background

diag_stripe(top,["black", "white"],100,8) ## cols redundant for square
    
currentframe = mainframe = create_main()
helpframe = create_help()
optionsframe = create_options()
gameframe = create_game()

##go_main()

root.mainloop()

Maybe the squarelist need to be global or it is not needed at all.

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.