不能从另一个函数中调用一个函数

can't call a function from within another function

您好,我正在尝试使用 tkinter 创建一个带有按钮的页面,按下该按钮会将该页面上的输入框状态从只读更改为正常。

这是我的代码,其中按钮调用的函数在 class 我的脚本仍在运行,但按钮什么都不做:

class playlist(tkinter.Frame): #creates playlist page              
def __init__(self, parent, controller):
    tkinter.Frame.__init__(self, parent)


    A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
    AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
    B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216",  relief="sunken", 
                        font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
    B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
                        font=("Elephant", 24), text="Edit", command=lambda: edit)
    E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)

    E1.pack()
    A4.pack(fill="both", expand=True)
    AC.pack()
    B1.pack()
    B2.pack()

    A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
    A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
    A4.create_window(960, 80, anchor="center", window=E1)
    A4.create_window(650, 80, anchor="center", window=B2)

def edit():    
    E1.configure(state = "normal")
    B2.configure(text="Done")
    E1.pack()
    B2.pack()
    App.update_idletasks()

这样函数就无法定义 E1 或 B2 所以我尝试了一个函数在第一个函数中的版本:

class playlist(tkinter.Frame): #creates playlist page              
def __init__(self, parent, controller):
    tkinter.Frame.__init__(self, parent)


    A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
    AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
    B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216",  relief="sunken", 
                        font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
    B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
                        font=("Elephant", 24), text="Edit", command=lambda: edit)
    E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)

    E1.pack()
    A4.pack(fill="both", expand=True)
    AC.pack()
    B1.pack()
    B2.pack()

    A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
    A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
    A4.create_window(960, 80, anchor="center", window=E1)
    A4.create_window(650, 80, anchor="center", window=B2)

    def edit():    
        E1.configure(state = "normal")
        B2.configure(text="Done")
        E1.pack()
        B2.pack()
        App.update_idletasks()

这样脚本仍然运行,按钮仍然不执行任何操作。 没有错误只是不工作。 提前致谢:)

我的主要建议是先看看没有 tkinter 的 python classes,然后在返回之前熟悉构建 class。

基本上,您需要将 tkinter 小部件分配给 class 的属性,以便可以从 class 中的任何其他地方引用它们。 IE。当您创建以后必须在 edit 方法中引用的按钮时,将它们分配给属性:

self.B1 = tkinter.Button(self, ...)
self.B2 = tkinter.Button(self, ...)

然后为了您的 edit 方法能够稍后访问它们,class 实例 self 被传递给它,所以您必须给您的编辑方法一个self参数,即

def edit(self):
    # then you can reference your attributes which store the widgets
    self.B1.config(...)

最后,按钮 self.B2 的命令只需要引用 edit 方法,不需要 lambda 语句,即

Button(...command=self.edit)