首页 > 解决方案 > 不能从另一个函数中调用一个函数

问题描述

嗨,我正在尝试使用 tkinter 创建一个带有按钮的页面,当按下该按钮时,该页面上的条目框的状态将从只读更改为正常。

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

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

这样脚本仍然运行,按钮仍然什么都不做。没有错误只是不工作。提前致谢 :)

标签: tkinterpython-3.7

解决方案


我的主要建议是先看看没有 tkinter 的 python 类,然后在回到它之前熟悉构建一个类。

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

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

然后,为了让您的edit方法能够稍后访问它们,将类实例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)

推荐阅读