Python Forum
destroy function issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
destroy function issue
#1
from tkinter import *
from math import *
root = Tk()

# Make window
root.geometry()

#mg/ml text entry MG/ML
mg2 = StringVar()
mg = Entry(root,textvariable=mg2,width=10, bg="white")
mg.grid(row=3,column=3,sticky=W)


#text entry WEIGHT
WEIGHT2 = StringVar()
WEIGHT = Entry(root, textvariable=WEIGHT2,width=10, bg="white")
WEIGHT.grid(row=1,column=3,sticky=W)

#text entry DOSE
dose2 = StringVar()
DOSE2 = Entry(root,textvariable=dose2,width=10, bg="white")
DOSE2.grid(columnspan=3,row=2,column=4,sticky=W)


#dose label>>
dose = Label(root, text='Dose ')
dose.grid(columnspan=2,row=2,column=0,sticky=W)

#weight label>>
Dog_weight = Label(root, text='What is the animals weight?')
Dog_weight.grid(columnspan=2,row=1,column=0,sticky=W)

#drug mg/ml label>>

drug_label = Label(root, text='How many mg are in a ml of this drug?')
drug_label.grid(columnspan=3,row=3,column=0,sticky=W)

#MATH

def ding():
   global t
   global cat2 
   time = 24  
   Weight3 = WEIGHT2.get()
   Weight3 = float(Weight3)
        
   dose3 = dose2.get()
   dose3 = float(dose3)

   mg3 = mg2.get()
   mg3 = float(mg3)
   math = Weight3 * dose3 / time / mg3
           
   t = float(math)
                 
   cat2 = Label(root, text=t, bg="white")
   cat2.grid(row=9,column=3,sticky=W)
   
button = Button(root, text="ml or mg", command=ding)
button.grid(row=7,column=3)

#clear button
def clear():
    global t
    global cat2
 
    try:
        t.destroy()
    except:
        pass
    try:
        cat2.destroy()
    except:
        pass

button_clear = Button(root, text='clear',command=clear)
button_clear.grid(row=10,column=1)
In this code the clear button works fine until you change a variable and recalculate before clearing. Then the clear button breaks. So I`m wondering what the issue is?
Reply
#2
(Jan-24-2018, 05:05 PM)SmokerX Wrote: Then the clear button breaks.
what exactly does 'breaks' mean? post full traceback in error tags if you get any
Reply
#3
(Jan-24-2018, 05:05 PM)SmokerX Wrote: So I`m wondering what the issue is?
If you don't let exceptions propagate by using things like
    try:
        t.destroy()
    except:
        pass
then you will have many mysterious issues. A good rule is to ignore exceptions only when you know that they can be safely ignored. You could replace this temporarily with
    try:
        t.destroy()
    except:
        raise
Reply
#4
Hi buran. There is no error tags that this issue triggers. What I mean by "breaks" is when you change a variable and a function button runs twice in a row without the clear button being pressed the clear button refuses to destroy labels. If you run through the function buttons only once the clear button destroys the labels, if that makes any sense. btw this is not the full code. I shortened it to the least amount of code to try and isolate the issue.

Thanks for the reply Gribouillis. I`ll try your suggestion and see what happens. Then google raise and see what it's doing. The help is much appreciated.

Ok I tried it. Again, thank you very much Gribouillis that helped alot to identify the problem. The problem was I was attempting to destroy a float and float object have no attribute destroy. So i'm off to figure out how to make it forget the float object and see if it debugs the clear function.

Ok i tried it, thanks again Gribouillis, I think this helped to identify the problem. The problem appears to be Im trying to destroy a float object when it has no such attribute. So im off to figure out how to get the app. to forget a float object and see if that clears up the destroy label function issue.

Ok i tried it, thanks again Gribouillis, I think this helped to identify the problem. The problem appears to be Im trying to destroy a float object when it has no such attribute. So im off to figure out how to get the app. to forget a float object and see if that clears up the destroy label function issue.
Reply
#5
Your experimenting the evil of global variables. At line 54 you have t = float(...) and at line 68 you have t.destroy(). Clearly, t is not a widget, so it doesn't need to be destroyed with the .destroy() method. You can remove the part of the code that destroys t.
Reply
#6
You are right, without calling raise I would have never realized I was not clearing t value from the memory. I have attempted to find float value attributes but scrapped it, tried converting it to clear its memory then scrapped that. Finally deleted if from the code. to see if it clears up the clear button:

from tkinter import *
from math import *
root = Tk()

# Make window
root.geometry()

#mg/ml text entry MG/ML
mg2 = StringVar()
mg = Entry(root,textvariable=mg2,width=10, bg="white")
mg.grid(row=3,column=3,sticky=W)


#text entry WEIGHT
WEIGHT2 = StringVar()
WEIGHT = Entry(root, textvariable=WEIGHT2,width=10, bg="white")
WEIGHT.grid(row=1,column=3,sticky=W)

#text entry DOSE
dose2 = StringVar()
DOSE2 = Entry(root,textvariable=dose2,width=10, bg="white")
DOSE2.grid(columnspan=3,row=2,column=4,sticky=W)


#dose label>>
dose = Label(root, text='Dose ')
dose.grid(columnspan=2,row=2,column=0,sticky=W)

#weight label>>
Dog_weight = Label(root, text='What is the animals weight?')
Dog_weight.grid(columnspan=2,row=1,column=0,sticky=W)

#drug mg/ml label>>

drug_label = Label(root, text='How many mg are in a ml of this drug?')
drug_label.grid(columnspan=3,row=3,column=0,sticky=W)

#MATH

def ding():
   global cat2
   try: cat2.destroy
   except:
      pass
   time = 24  
   Weight3 = float(WEIGHT2.get())
   
   dose3 = float(dose2.get())
   

   mg3 = float(mg2.get())
   
   math = Weight3 * dose3 / time / mg3
           
   t = float(math)
                 
   cat2 = Label(root, text=t, bg="white")
   cat2.grid(row=9,column=3,sticky=W)
   
button = Button(root, text="ml or mg", command=ding)
button.grid(row=7,column=3)
   
#clear button
def clear():
    global cat2
    
    try:
        cat2.destroy()
    except:
        raise

button_clear = Button(root, text='clear',command=clear)
button_clear.grid(row=10,column=1)
Notice I`m even trying to destroy cat2 within the command and yet the problem persists and the clear function only destroys the freshest instance of cat2. I am too uneducated to determine if this is a bug.
Reply
#7
You don't need to destroy the widget cat2. You can simply hide it by removing it from the grid. I changed your code a little to do this and clear the entries too
from tkinter import *
from math import *
root = Tk()
 
# Make window
root.geometry()
 
#mg/ml text entry MG/ML
mg2 = StringVar()
mg = Entry(root,textvariable=mg2,width=10, bg="white")
mg.grid(row=3,column=3,sticky=W)
 
 
#text entry WEIGHT
WEIGHT2 = StringVar()
WEIGHT = Entry(root, textvariable=WEIGHT2,width=10, bg="white")
WEIGHT.grid(row=1,column=3,sticky=W)
 
#text entry DOSE
dose2 = StringVar()
DOSE2 = Entry(root,textvariable=dose2,width=10, bg="white")
DOSE2.grid(columnspan=3,row=2,column=4,sticky=W)
 
 
#dose label>>
dose = Label(root, text='Dose ')
dose.grid(columnspan=2,row=2,column=0,sticky=W)
 
#weight label>>
Dog_weight = Label(root, text='What is the animals weight?')
Dog_weight.grid(columnspan=2,row=1,column=0,sticky=W)
 
#drug mg/ml label>>
 
drug_label = Label(root, text='How many mg are in a ml of this drug?')
drug_label.grid(columnspan=3,row=3,column=0,sticky=W)

cat2 = Label(root, text='', bg="white")
 
#MATH
 
def ding():
   time = 24  
   Weight3 = float(WEIGHT2.get())
    
   dose3 = float(dose2.get())
    
 
   mg3 = float(mg2.get())
    
   math = Weight3 * dose3 / time / mg3
            
   t = float(math)
   cat2.configure(text=t)
   cat2.grid(row=9,column=3,sticky=W)
    
button = Button(root, text="ml or mg", command=ding)
button.grid(row=7,column=3)
    
#clear button
def clear():
    WEIGHT2.set('')
    dose2.set('')
    mg2.set('')
    cat2.grid_remove()
 
button_clear = Button(root, text='clear',command=clear)
button_clear.grid(row=10,column=1)

root.mainloop()
Please, configure your editor to indent python code with 4 space characters! This is the most widely accepted convention because it is used by the language's developers.
Reply
#8
thanks Gribouillis. I love you. By using your knowledge it also seems to work like this:

from tkinter import *
from math import *
root = Tk()
 
# Make window
root.geometry()
 
#mg/ml text entry MG/ML
mg2 = StringVar()
mg = Entry(root,textvariable=mg2,width=10, bg="white")
mg.grid(row=3,column=3,sticky=W)
 
 
#text entry WEIGHT
WEIGHT2 = StringVar()
WEIGHT = Entry(root, textvariable=WEIGHT2,width=10, bg="white")
WEIGHT.grid(row=1,column=3,sticky=W)
 
#text entry DOSE
dose2 = StringVar()
DOSE2 = Entry(root,textvariable=dose2,width=10, bg="white")
DOSE2.grid(columnspan=3,row=2,column=4,sticky=W)
 
 
#dose label>>
dose = Label(root, text='Dose ')
dose.grid(columnspan=2,row=2,column=0,sticky=W)
 
#weight label>>
Dog_weight = Label(root, text='What is the animals weight?')
Dog_weight.grid(columnspan=2,row=1,column=0,sticky=W)
 
#drug mg/ml label>>
 
drug_label = Label(root, text='How many mg are in a ml of this drug?')
drug_label.grid(columnspan=3,row=3,column=0,sticky=W)
 
#MATH
 
def ding():
   global cat2
   try: cat2.grid_remove()
   
   except:
      pass
   time = 24  
   Weight3 = float(WEIGHT2.get())
    
   dose3 = float(dose2.get())
    
   mg3 = float(mg2.get())
    
   math = Weight3 * dose3 / time / mg3
            
   t = float(math)
                  
   cat2 = Label(root, text=t, bg="white")
   cat2.grid(row=9,column=3,sticky=W)
    
button = Button(root, text="ml or mg", command=ding)
button.grid(row=7,column=3)
    
#clear button
def clear():
    global cat2
     
    try:
        cat2.destroy()
    except:
        raise
 
button_clear = Button(root, text='clear',command=clear)
button_clear.grid(row=10,column=1)
If you just have to destroy stuff for whatever reason.
Reply
#9
(Jan-26-2018, 09:26 PM)SmokerX Wrote: If you just have to destroy stuff for whatever reason.
There is really no reason to destroy cat2 during the clear action. It is sufficient to hide it.

Tip: Avoid except: pass. It is only a way to hide problems. The consequence is that your program becomes harder to debug. Exceptions are the best friends of the python programmer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,545 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 773 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,543 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  Menu destroy Heyjoe 5 3,351 Mar-02-2021, 01:45 AM
Last Post: Heyjoe
  clear button destroy can't work jacklee26 1 4,093 Jul-07-2019, 03:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020