Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

python-3.x - 在python中使用lambda表达式调用另一个类的方法(Calling a Method of another class with a lambda expression in python)

I am trying to create a little GUI with multiple pages.

(我正在尝试创建一个带有多个页面的GUI。)

The First page has a button which raises the second page and changes the label text on the second page.

(第一页上有一个按钮,该按钮可升高第二页并更改第二页上的标签文本。)

However, I fail to call the method of the second page which is supposed to change the text.

(但是,我无法调用应该更改文本的第二页的方法。)

Can somebody tell me, why I get the following error when calling the method?

(有人可以告诉我,为什么调用该方法时出现以下错误?)

TypeError: changeLabel() missing 1 required positional argument: 'self'

(TypeError:changeLabel()缺少1个必需的位置参数:“ self”)

import tkinter as tk


class ExampleApp(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack()

        self.frames = {}

        for F in (FirstPage, SecondPage):

            frame = F(container, self)

            self.frames[F] = frame

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

    def show_frame(self, cont):

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


class FirstPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        self.buttonFP = tk.Button(self, text="Next Page",
                                  command=lambda : [f() for f in [SecondPage.changeLabel(),
                                                                  controller.show_frame(SecondPage)]])
        self.buttonFP.pack()



class SecondPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        self.label = tk.Label(self, text="Test")

        self.label.pack()


    def changeLabel(self):
        """change text of label"""

        newLabel = "changed"
        self.label.configure(text=newLabel)




if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()
  ask by Max2603 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...