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

时间:2018-10-18 19:21:55

标签: tkinter python-3.7

嗨,我正在尝试使用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()

这样,脚本仍然运行,并且按钮仍然不执行任何操作。 没有错误就是行不通。 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我的主要建议是先查看不带tkinter的python类,并在返回之前熟悉构建一个类。

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

edit

然后,为了让您的self.B1 = tkinter.Button(self, ...) self.B2 = tkinter.Button(self, ...) 方法能够在以后访问它们,将类实例edit传递给它,因此您必须给您的edit方法一个self参数,即。

self

最后,按钮def edit(self): # then you can reference your attributes which store the widgets self.B1.config(...) 的命令只需要引用self.B2方法,就不需要lambda语句。

edit