-1

I am creating a Reset button of a GUI by using the Tkinter. My purpose is that reset all values of labels to 0 or disappear.

To reset value of labels, I defined the labels as global variable.

labelP = None
labelV = None
labelH = None
labelT = None

Main function:

def predict():
    global labelP
    global labelV
    global labelH
    global labelT
...
    labelP = Label(mainframe, text = np.array(A[1,0]), width=5, font = 'Arial 10 bold').grid(column=2, row=5, sticky=W)
    labelV = Label(mainframe, text = np.array(A[1,1]), width=5, font = 'Arial 10 bold').grid(column=2, row=6, sticky=W)
    labelH = Label(mainframe, text = np.array(A[1,2]), width=5, font = 'Arial 10 bold').grid(column=2, row=7, sticky=W)
    labelT = Label(mainframe, text = np.array(A[1,3]), width=5, font = 'Arial 10 bold').grid(column=2, row=8, sticky=W)
...

reset function:

def reset():
    global labelP
    global labelV
    global labelH
    global labelT

    labelP.delete()
    labelV.delete()
    labelH.delete()
    labelT.delete()

Reset button:

btn2 = Button(mainframe, text="Reset", command = reset).grid(column=1, row=9, sticky=W)

Although I also tried labelP.config(), labelP.set(""), labelP.destroy() but I always get the error:

AttributeError: 'NoneType' object has no attribute ...

What should I define for the global variables?

Thank you This is my code: https://gist.github.com/TonyS0n/8ee8db392196a405c2ef27ac44df3059

Tony
  • 3
  • 4

1 Answers1

1

You can't use a single line to initialize and layout a widget. You have to put that on 2 lines, like this:

labelP = Label(mainframe, text = np.array(A[1,0]), width=5, font = 'Arial 10 bold')
labelP.grid(column=2, row=5, sticky=W)
Novel
  • 13,406
  • 2
  • 25
  • 41
  • Thank you very much. My problem is solved because of your suggestion. However, I do not clearly understand the difference between single line and 2 lines. I want to understand deeper. Can you explain more, please ? – Tony Feb 03 '18 at 17:09
  • 1
    @Tony What's there to explain? `Label(...)` returns a label, `grid(...)` returns `None`. – Aran-Fey Feb 03 '18 at 17:12