-1

I'm writing a Tkinter Application. I'm trying to delete a Label by using destroy() but it is not working correctly.

What would be the correct way to delete (or remove) the Label.

Base code:

typeimport tkinter
from tkinter import *
from tkinter import ttk

window=Tk()
window.minsize(300,200)
window.title("COMBO 3 PIECE 1 SODA")

Label(text="UFC make fight",font=20,pady=10 ,padx=10).grid(row=0,column=0,sticky=W)
opponet=StringVar(value="คู่ต่อสู้ของคุณ")
combo2=ttk.Combobox(textvariable=opponet)
combo2["value"]=("Conor Mcgregor","Jorge Masvidal","Colby Covington","Tony Ferguson","Sean O malley")
combo2.grid(row=0,column=2)

choice = StringVar(value="เลือกนักสู้ของคุณ")
combo=ttk.Combobox(textvariable=choice)
combo["value"]=("Conor Mcgregor","Jorge Masvidal","Colby Covington","Tony Ferguson","Sean O malley")
combo.grid(row=0,column=1)

#betodd2 = IntVar()
#betset2=Entry(font=30,width=10,textvariable=betodd2).grid(row=1,column=2)
Label(text="ประเมินทักษะมวยปล้ำ",font=20,pady=10 ,padx=10).grid(row=1,column=0,sticky=W)
Label(text="ค่าทักษะมวยสากล",font=20,pady=10 ,padx=10).grid(row=2,column=0,sticky=W)
Label(text="ค่าทักษะมวยไทย",font=20,pady=10 ,padx=10).grid(row=3,column=0,sticky=W)
Label(text="ค่าทักษะBJJ",font=20,pady=10 ,padx=10).grid(row=4,column=0,sticky=W)
WrestlingPoint1 = IntVar()
Spinbox(from_=1,to=10,textvariable=WrestlingPoint1).grid(row=1,column=1)
WrestlingPoint2 = IntVar()
Spinbox(from_=1,to=10,textvariable=WrestlingPoint2).grid(row=1,column=2)
BoxingPoint1 = IntVar()
Spinbox(from_=1,to=10,textvariable=BoxingPoint1).grid(row=2,column=1)
BoxingPoint2 = IntVar()
Spinbox(from_=1,to=10,textvariable=BoxingPoint2).grid(row=2,column=2)
MuayThaiPoint1 = IntVar()
Spinbox(from_=1,to=10,textvariable=MuayThaiPoint1).grid(row=3,column=1)
MuayThaiPoint2 = IntVar()
Spinbox(from_=1,to=10,textvariable=MuayThaiPoint2).grid(row=3,column=2)
BJJPoint1 = IntVar()
Spinbox(from_=1,to=10,textvariable=BJJPoint1).grid(row=4,column=1)
BJJPoint2 = IntVar()
Spinbox(from_=1,to=10,textvariable=BJJPoint2).grid(row=4,column=2)

Label(text="รวมผลคะแนน",font=20,pady=10 ,padx=10).grid(row=5,column=0,sticky=W)

Label(text="คะแนนนักสู้คนที่ 1 ",font=20,pady=10 ,padx=10).grid(row=6,column=1)
Label(text="คะแนนนักสู้คนที่ 2 ",font=20,pady=10 ,padx=10).grid(row=6,column=2)

content1=""
content2=""
txt_input= StringVar(value="0")
txt_input2= StringVar(value="0")

TOTAL1=Entry(font=('impact',15,'bold'),fg='black',bg='red',width=10,textvariable=txt_input).grid(row=5, column=1)
TOTAL2=Entry(font=('impact',15,'bold'),fg='black',bg='red',width=10,textvariable=txt_input2).grid(row=5, column=2)

going=Label(window)
going2=Label(window)
#summary=Entry(font=('impact',15,'bold'),fg='black',bg='red',width=10,textvariable=txt_input).grid(row=10, column=0,columnspan=0)

The clearcommand function:

def clearcommand():
    global content1
    content1 = ""
    global content2
    content2 = ""
    txt_input = StringVar(value="0")
    txt_input2 = StringVar(value="0")
    Label.destroy()
    TOTAL1 = Entry(font=('impact', 15, 'bold'), fg='black', bg='red', width=10, textvariable=txt_input).grid(row=5,
                                                                                                             column=1)
    TOTAL2 = Entry(font=('impact', 15, 'bold'), fg='black', bg='red', width=10, textvariable=txt_input2).grid(row=5,
                                                                                                              column=2)

    confirm['state']=NORMAL

clear=Button(text="เคลียร์ข้อมูล",command=clearcommand,font=30,background='red').grid(row=11,column=2)
Viraj Shah
  • 754
  • 5
  • 19
  • I've already edited the post, but you will need to trim down your code even more. Make sure to use clear English, not fill your question with fluff, and provide a minimal reproducible example. – Viraj Shah Mar 17 '23 at 20:40
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Mar 17 '23 at 20:40

2 Answers2

1

None of your Label widgets are being stored in variables, so you're not going to be able to modify (or destroy) them after the fact.

You should store the Label you want to update in a variable, then if you just want to clear out the text of the label without actually destroying the widget, you can modify clearcommand like this:

my_label = Label(...)  # label to be modified, code omitted for brevity
my_label.grid(...)  # put the label on the grid, code omitted for brevity

# you need to add the label to the grid *separately* from declaring it, otherwise
# 'my_label' will be 'None' instead of a reference to the Label object!

# this is because 'grid()' returns 'None', so if you do 'my_label = Label(...).grid(...)', 
# then 'my_label' will evaluate to 'None', which is not what you want!


def clearcommand():
    global content1
    content1 = ""
    global content2
    content2 = ""
    txt_input = StringVar(value="0")
    txt_input2 = StringVar(value="0")

    # set the label's text to an empty string
    my_label.config(text='')

    TOTAL1 = Entry(font=('impact', 15, 'bold'), fg='black', bg='red', width=10, textvariable=txt_input).grid(row=5,column=1)
    TOTAL2 = Entry(font=('impact', 15, 'bold'), fg='black', bg='red', width=10, textvariable=txt_input2).grid(row=5, column=2)

Updating the label this way means that the widget still exists and can be changed again later

JRiggles
  • 4,847
  • 1
  • 12
  • 27
0

i try to delete Label but i can't delete

  • In line 25 - 55, every widget must be assigned to variables.
  • Every widget must split into two lines instead of one line to prevent errors.
  • In line 25 - 55, every widget must be assigned to variables
  • In line 62, 63, 82 and 87, comment out #content1="" and #content1="" So we don't need global.
  • In line 91 and 92 comment out #going=Label(window) #going2=Label(window). We don't know what is this doing.
  • Comment out all #global in functions
  • In line 113, 117 and 120, Replace Label with lbl_x.configure. x denote index

In clearcommand().

  • Add Lbl_x.destroy() x denote index
  • Add delete for Entry
  • Add IntVar() to variables for every Spinbox and sbx.configure. the x denote index
  • In line 158, lbl_4 = Label outside function.

Snippet:

from tkinter import *
from tkinter import ttk

window=Tk()
window.minsize(300,200)
window.title("COMBO 3 PIECE 1 SODA")

Label(text="UFC make fight",font=20,pady=10 ,padx=10).grid(row=0,column=0,sticky=W)
opponet=StringVar(value="คู่ต่อสู้ของคุณ")
combo2=ttk.Combobox(textvariable=opponet)
combo2["value"]=("Conor Mcgregor","Jorge Masvidal","Colby Covington","Tony Ferguson","Sean O malley")
combo2.grid(row=0,column=2)

choice = StringVar(value="เลือกนักสู้ของคุณ")
combo=ttk.Combobox(textvariable=choice)
combo["value"]=("Conor Mcgregor","Jorge Masvidal","Colby Covington","Tony Ferguson","Sean O malley")
combo.grid(row=0,column=1)


Label(text="ประเมินทักษะมวยปล้ำ",font=20,pady=10 ,padx=10).grid(row=1,column=0,sticky=W)
Label(text="ค่าทักษะมวยสากล",font=20,pady=10 ,padx=10).grid(row=2,column=0,sticky=W)
Label(text="ค่าทักษะมวยไทย",font=20,pady=10 ,padx=10).grid(row=3,column=0,sticky=W)
Label(text="ค่าทักษะBJJ",font=20,pady=10 ,padx=10).grid(row=4,column=0,sticky=W)

WrestlingPoint1 = IntVar()
sb1 = Spinbox(from_=1,to=10,textvariable=WrestlingPoint1)
sb1.grid(row=1,column=1)

WrestlingPoint2 = IntVar()
sb2 = Spinbox(from_=1,to=10,textvariable=WrestlingPoint2)
sb2.grid(row=1,column=2)

BoxingPoint1 = IntVar()
sb3 = Spinbox(from_=1,to=10,textvariable=BoxingPoint1)
sb3.grid(row=2,column=1)

BoxingPoint2 = IntVar()
sb4 = Spinbox(from_=1,to=10,textvariable=BoxingPoint2)
sb4.grid(row=2,column=2)

MuayThaiPoint1 = IntVar()
sb5 = Spinbox(from_=1,to=10,textvariable=MuayThaiPoint1)
sb5.grid(row=3,column=1)

MuayThaiPoint2 = IntVar()
sb6 = Spinbox(from_=1,to=10,textvariable=MuayThaiPoint2)
sb6.grid(row=3,column=2)

BJJPoint1 = IntVar()
sb7 = Spinbox(from_=1,to=10,textvariable=BJJPoint1)
sb7.grid(row=4,column=1)

BJJPoint2 = IntVar()
sb8 = Spinbox(from_=1,to=10,textvariable=BJJPoint2)
sb8.grid(row=4,column=2)

Label(text="รวมผลคะแนน", font=20,pady=10 ,padx=10).grid(row=5,column=0,sticky=W)

Label(text="คะแนนนักสู้คนที่ 1 ",font=20,pady=10 ,padx=10).grid(row=6,column=1)
Label(text="คะแนนนักสู้คนที่ 2 ",font=20,pady=10 ,padx=10).grid(row=6,column=2)

#content1=""
#content2=""
txt_input= StringVar(value="0")
txt_input2= StringVar(value="0")

TOTAL1 = Entry(window, font=('impact', 15,'bold'),fg='black',bg='green',width=10,textvariable=txt_input)
TOTAL1.grid(row=5, column=1)
TOTAL2 = Entry(font=('impact', 15,'bold'),fg='black',bg='yellow',width=10,textvariable=txt_input2)
TOTAL2.grid(row=5, column=2)

lbl_1 = Label(text=f"ผลการวิเคราะห์คะแนนพบว่า {choice.get()} เป็น มวยต่อ(Favorite) \n {opponet.get()} เป็นมวยรอง",
                      font=('Arial',13),background='blue',fg='black')

lbl_2 = Label(text=f"ผลการวิเคราะห์คะแนนพบว่า {opponet.get()} เป็น มวยต่อ(Favorite) \n {choice.get()} เป็นมวยรอง", font=('Arial',13),background='green',fg='black')

lbl_3 = Label(text=f"ผลการวิเคราะห์คะแนนพบว่า {choice.get()} สูสีคู่คี่กันกับ {opponet.get()} ", font=('Arial',13),background='grey',fg='black')

 

def TOTALSCORE1():
    #global content1
    content1=WrestlingPoint1.get()+BoxingPoint1.get()+MuayThaiPoint1.get()+BJJPoint1.get()
    txt_input.set(content1)
    
def TOTALSCORE2():
    #global content2
    content2=WrestlingPoint2.get()+BoxingPoint2.get()+MuayThaiPoint2.get()+BJJPoint2.get()
    txt_input2.set(content2)

#going=Label(window)
#going2=Label(window)
#summary=Entry(font=('impact',15,'bold'),fg='black',bg='red',width=10,textvariable=txt_input).grid(row=10, column=0,columnspan=0)

def comparescore():
    #global going
    total1 = WrestlingPoint1.get() + BoxingPoint1.get() + MuayThaiPoint1.get() + BJJPoint1.get()
    total2 = WrestlingPoint2.get() + BoxingPoint2.get() + MuayThaiPoint2.get() + BJJPoint2.get()


def pickfighter():
     
    lbl_4.configure(text=f"{choice.get()} ปะทะ {opponet.get()}",font=('impact',13),background='aqua',fg='black')
    lbl_4.grid(row=9,column=0)
    
    TOTALSCORE1()
    TOTALSCORE2()
    
    total1 = WrestlingPoint1.get() + BoxingPoint1.get() + MuayThaiPoint1.get() + BJJPoint1.get()
    total2 = WrestlingPoint2.get() + BoxingPoint2.get() + MuayThaiPoint2.get() + BJJPoint2.get()
    
    if total1 > total2:
        lbl_1.configure(text=f"ผลการวิเคราะห์คะแนนพบว่า {choice.get()} เป็น มวยต่อ(Favorite) \n {opponet.get()} เป็นมวยรอง",
                      font=('Arial',13),background='blue',fg='black') 
        lbl_1.grid(row=10, column=0, columnspan=2)
    elif total2 > total1:
        lbl_2.configure(text=f"ผลการวิเคราะห์คะแนนพบว่า {opponet.get()} เป็น มวยต่อ(Favorite) \n {choice.get()} เป็นมวยรอง", font=('Arial',13),background='green',fg='black')
        lbl_2.grid(row=10, column=0, columnspan=2)
    elif total1 == total2:
        lbl_3.configure(text=f"ผลการวิเคราะห์คะแนนพบว่า {choice.get()} สูสีคู่คี่กันกับ {opponet.get()} ", font=('Arial',13),background='grey',fg='black')
        lbl_3.grid(row=10, column=0, columnspan=2)
     

def clearcommand():
    lbl_1.destroy()
    lbl_2.destroy()
    lbl_3.destroy()
    lbl_4.destroy()
     
     
    TOTAL1.delete(0, END )                                                                                                           
    TOTAL2.delete(0, END )
    
    WrestlingPoint1 = IntVar(value=0)
    sb1.configure(textvariable=WrestlingPoint1)

    WrestlingPoint2 = IntVar()
    sb2.configure(textvariable=WrestlingPoint2)

    BoxingPoint1 = IntVar()
    sb3.configure(textvariable=BoxingPoint1)

    BoxingPoint2 = IntVar()
    sb4.configure(textvariable=BoxingPoint2)

    MuayThaiPoint1 = IntVar()
    sb5.configure(textvariable=MuayThaiPoint1)

    MuayThaiPoint2 = IntVar()
    sb6.configure(textvariable=MuayThaiPoint2)

    BJJPoint1 = IntVar()
    sb7.configure(textvariable=BJJPoint1)

    BJJPoint2 = IntVar()
    sb8.configure(textvariable=BJJPoint2)

lbl_4 = Label(text=f"{choice.get()} ปะทะ {opponet.get()}",font=('impact',13),background='yellow',fg='black')
    
confirm=Button(text="ยืนยัน",command=pickfighter,font=30,background='blue').grid(row=11,column=0)
clear=Button(text="เคลียร์ข้อมูล",command=clearcommand,font=30,background='red').grid(row=11,column=2)

window.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19