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

时间:2022-07-31 23:56:36

I have a code which open a main frame window with 2 buttons and a counter. If press Open Msg it show up a message and if i choose yes i open a new frame. If i press Open Page ZS it opens new frame also. My issue is that when i try to open that new frame from inside the function that do counting i get "NameError: global name 'controller' is not defined". Thank you in advance

我有一个代码打开一个主框架窗口,有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 个解决方案

#1


You're trying to use controller without defining it.

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

controller.show_frame(PageZS)

I would suggest adding

我建议补充一下

self.controller = controller 

Under the init function and use

在init函数下使用

self.controller.show_frame(...)

#1


You're trying to use controller without defining it.

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

controller.show_frame(PageZS)

I would suggest adding

我建议补充一下

self.controller = controller 

Under the init function and use

在init函数下使用

self.controller.show_frame(...)