Count not defined

Name count is not defined, I’m not sure why this is

            def diary_search():
                #close found diary window
                found_diary.destroy()
                #create diary
                global diary_window
                diary_window = tkinter.Toplevel()
                #change diary window title
                diary_window.title("DIARY")
                #change diary room window size
                diary_window.geometry("600x400")
                #change diary window colour
                diary_window.configure(bg = "white")
                #create frames
                top_frame13 = tkinter.Frame(diary_window, bg = "white")
                mid_frame13 = tkinter.Frame(diary_window, bg = "white")
                bottom_frame13 = tkinter.Frame(diary_window)
                #pack frames
                top_frame13.pack()
                mid_frame13.pack()
                bottom_frame13.pack(pady=(20,10))

                #create page frames
                
                page1_frame = tkinter.Frame(top_frame13)
                pg1 = tkinter.Label(page1_frame, text = "DIARY", bg = "white", font = ("arial", 10))
                page2_frame = tkinter.Frame(top_frame13)
                pg2 = tkinter.Label(page2_frame, text = "I love her I do, but she says she needs the money. If only he was gone, and I can make sure of it...", bg = "white", font = ("arial", 10))
                page3_frame = tkinter.Frame(top_frame13)
                pg3 = tkinter.Label(page3_frame, text = "I can't belive what happened today...He's gone now but...", bg = "white", font = ("arial", 10))
                page4_frame = tkinter.Frame(top_frame13)
                pg4 = tkinter.Label(page4_frame, text = "BACK, 1882", bg = "white", font = ("arial", 10))
                #make pages list
                pages = [page1_frame, page2_frame, page3_frame, page4_frame]
                #make count
                count = 0
                #def next and back
                def next_page():
                    global count
                    if not count > len(pages) - 2:
                     for p in pages:
                         p.pack_forget()
                    count += 1
                    # counts how many pages
                    page = pages[count]
                    page.pack(pady=100)

                def back_page():
                    global count
                    if not count == 0:
                        for p in pages:
                            p.pack_forget()
                    count -= 1
                    # counts how many pages
                    page = pages[count]
                    page.pack(pady=100)
                
                #pack top frame widgets
                page1_frame.pack(pady=100)
                pg1.pack()
                pg2.pack()
                pg3.pack()
                pg4.pack()

                #create mid frame widgets

                #create back and next buttons
                back_button = tkinter.Button(mid_frame13, text = "Back", bg = "white", font = ("arial", 13), relief = "groove", bd = 3, command = back_page)
                next_button = tkinter.Button(mid_frame13, text = "Next", bg = "white", font = ("arial", 13), relief = "groove", bd = 3, command = next_page)
                #pack mid frame widgets
                back_button.pack(side = "left", padx=20)
                next_button.pack(side = "right", padx=20)

You’ve declared count as being global in next_page and back_page instead of nonlocal.

1 Like