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()
I am not an expert, but I believe the
label1
is lost as its not a globalYou could ask your
root
for all thewidgets
it contains, and thendestroy
thelabels
.