在相同的类中调用相同的函数但以不同的方式调用 - python

时间:2015-05-09 21:10:05

标签: python tkinter

我有一个代码,用2个按钮和一个计数器打开一个主框架窗口。如果按下打开消息它会显示一条消息,如果我选择是,则打开一个新帧。如果我按下Open Page ZS,它也会打开新的框架。我的问题是,当我尝试从计数函数内部打开新框架时,我得到“NameError:全局名称'控制器'未定义”。 提前谢谢

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


start_app = time.time()

LARGE_FONT= ("Verdana", 12)

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

#MainPage
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'
               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.initializaresenz=Label(f,text='',font=("Arial", 18, "bold"),fg='#ff9900',width=35,height=3)
        self.initializaresenz.grid(row=4,column=0,columnspan=6)

        self.initializarestatus1=Label(f,text='Start!',font=("Arial", 18, "bold"),fg='#00cc33',width=55,height=3)
        self.initializarestatus1.grid(row=5,column=0,columnspan=6)
        self.initializarestatus2=Label(f,text='Error!',font=("Arial", 18, "bold"),fg='#ff3300',width=55,height=3)
        self.initializarestatus2.grid(row=5,column=0,columnspan=6)
        self.initializarestatus1.grid_remove()
        self.initializarestatus2.grid_remove()

        self.update_labels()

    def update_labels(self):

        now=time.time()
        diff =15 - int(now-start_app)
        txt_diff = ''

        if diff < 0:
            diff = '00'

        if diff < 10:
            txt_diff='0'+str(diff)
        else:
            txt_diff=str(diff)

        if diff < 9:
            self.initializarestatus1.grid()
            self.initializarestatus2.grid_remove()
            controller.show_frame(PageZS)
        elif diff < 1:
            self.initializarestatus2.grid()
            self.initializarestatus1.grid_remove()

        self.initializaresenz.configure(text='Counting ... ' + txt_diff)

        self.after(10, self.update_labels)



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

1 个答案:

答案 0 :(得分:0)

您正在尝试使用控制器而不定义它。

controller.show_frame(PageZS)

我建议添加

self.controller = controller 

init 功能下并使用

self.controller.show_frame(...)