meowpath99
meowpath99

Reputation: 1

Can't remove text from label. Python, Tkinter

Cannot understand what i did wrong here. A function named clearlabel should clear text from label1, but it says its not defined. Can you show me what I did wrong here?

from tkinter import *
root = Tk()
w = (root.winfo_screenwidth() // 2) - 200
h = (root.winfo_screenheight() // 2) - 200
root.geometry('400x400+{}+{}'.format(w, h))
root.title("Pascal Triangle")



def PrintPasTriangle():
    row = [1]
    for i in range(int(ent.get())):
        label1 = Label(root, text=row, font = "Times 10")
        row = [sum(x) for x in zip([0]+row, row+[0])]
        label1.pack()
ent = Entry(root, width=50)
ent.pack()
ent.insert(0, "")


def clearlabel():
    label.delete("0", END)


myButton = Button(root, text="Show Triangle", font = "Times 10", command=PrintPasTriangle)
myButton.pack()

myButton2 = Button(root, text="Clear Triangle", font = "Times 10", command=clearlabel)
myButton2.pack()

Upvotes: 0

Views: 394

Answers (2)

Lachlan
Lachlan

Reputation: 370

I am not an expert, but I believe the label1 is lost as its not a global

You could ask your root for all the widgets it contains, and then destroy the labels.

from tkinter import *
root = Tk()
w = (root.winfo_screenwidth() // 2) - 200
h = (root.winfo_screenheight() // 2) - 200
root.geometry('400x400+{}+{}'.format(w, h))
root.title("Pascal Triangle")



def PrintPasTriangle():
    row = [1]
    for i in range(int(ent.get())):
        label1 = Label(root, text=row, font = "Times 10")
        row = [sum(x) for x in zip([0]+row, row+[0])]
        label1.pack()
ent = Entry(root, width=50)
ent.pack()
ent.insert(0, "")


def clearlabel():
    #label1.delete("0", END)
    for widget in root.winfo_children():
        if isinstance(widget, Label):
            widget.destroy()
myButton = Button(root, text="Show Triangle", font = "Times 10", command=PrintPasTriangle)
myButton.pack()

myButton2 = Button(root, text="Clear Triangle", font = "Times 10", command=clearlabel)
myButton2.pack()

Upvotes: 1

acw1668
acw1668

Reputation: 47194

There are few issues in your code:

  • label1 is a local variable inside PrintPasTriangle(), so it cannot be accessed outside the function
  • you have used same variable label1 for all the rows, so only the last one is referenced
  • you have used label instead of label1 inside clearlabel()
  • you have used undefined function .delete(0, END) on a label widget

I would suggest to create label1 once outside the function for the Pascal Triangle instead of using multiple labels and update its text inside the two functions using .config()

...

def PrintPasTriangle():
    row = [1]
    triangle = '1'
    for i in range(1, int(ent.get())):
        row = [sum(x) for x in zip([0]+row, row+[0])]
        triangle += "\n" + " ".join(str(x) for x in row)
    label1.config(text=triangle)

...

def clearlabel():
    label1.config(text='')

...

label1 = Label(root, font="Times 12")
label1.pack()

root.mainloop()

Upvotes: 1

Related Questions