I have this code which if i press msgbox yes it opens another frame but if i do same thing from another function (counter) i get an error. Can someone help me and tell what is wrong?

import tkMessageBox
import Tkinter as tk
from Tkinter import *
from functools import partial
import time

LARGE_FONT= ("Verdana", 12)

start_app = time.time()

class ChangePages(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (MainPage, PageZS):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class MainPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)
        def C(*args): return partial(self.option_changed, *args)

        f = Frame(self)
        f.pack(side='top')

        def zsmessage():

            result = tkMessageBox.askquestion('Open page ZS', 'Do you want open page ZS ?', icon='warning')

            if result == 'yes':
                print 'Open page ZS'
                #self.command=lambda:
                controller.show_frame(PageZS)
            else:
                print 'NO'

        btnzs = Button(f,text='Open MSG',fg='blue',font=('Helvetica',26),height=1, width=25,command=zsmessage)
        btnzs.grid(row=2,column=1)

        btnzsold=Button(f,text='Open Page ZS',fg='blue',font=('Helvetica',26),height=1, width=25,command=lambda: controller.show_frame(PageZS))
        btnzsold.grid(row=2,column=5)

        self.counter()

    def counter(self):
        now=time.time()
        passed = 10 - int(now-start_app)
        print passed

        if passed < 5:
            controller.show_frame(PageZS)

        self.after(10, self.counter)

class PageZS(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        f = Frame(self)
        f.pack(side='left')

        labelspacing=Label(f,text='PAGE ZS',fg='red',font=("Arial", 12, "bold"),width=15,height=0,relief='groove').grid(row=0,column=0)

app = ChangePages()
app.geometry('1000x500+0+0')
app.title('Title ')
app.mainloop()

Thanks in advance

The controller variable is not defined within the counter method. You need to either pass it as a parameter or make it a member of the object (by doing self.controller = controller in the constructor) and then access it as self.controller instead.

In the future please include any error messages you get with your code (including line numbers and such). This makes it much easier for us to find the problem.

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.