'int' object has no attribute 'delete'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cj007541
    New Member
    • Apr 2009
    • 6

    'int' object has no attribute 'delete'

    Hey. Im working on this for a school project thing and i need some help please.

    Code:
    from Tkinter import*
    import random
    import time
    
    number = 5
    i = 0
    
    class Test():
        def __init__(self):
            i=0
            self.root = Tk()
            self.root.title('Test')
            
            self.can = Canvas(self.root, width= 800, height = 600, bg='Blue')
            self.can.grid()
    
            self.b = Button(self.root, width=25, height = 5, text="START TEST", command=self.countdown)
            self.b.grid(column=0, row=0)
            
            self.root.mainloop()
    
    
    
    
        def countdown(self):
            global i
            global number
    
            if i == 0:
                self.b.destroy()
    
                self.text=self.can.create_text(400, 300, text='TEST STARTS IN...')
                i+=1
                self.countdown()
            elif i>0 and i<6:
                self.text=self.can.create_text(400, 325, text=number)
                time.sleep(1)
                self.text.delete()
                self.root.update()
                number=number-1
                self.countdown()
            else:
                pass
    
    
    
     
    
    
    Test()
    Thank you.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I know very little about Tkinter. I was able to get the script to work by making some changes. First, get rid of the global variables number and i. In many cases, global variables are unnecessary and their use is typically discouraged. Second, eliminate the recursive calls to self.countdown( ). Third, the delete() method is a canvas method, not a method of the text ID, which is an integer. Fourth, use a for loop to make the countdown.
    Code:
            self.b.destroy()
            for i in range(6, 0, -1):
                if i == 6:
                    ** do stuff **
                else:
                    ** do other stuff **
            self.root.destroy()
    I added the destroy() method to close the canvas window, but you may need to do something else. Since this is a school project, I won't provide the complete code.

    -BV

    Comment

    • cj007541
      New Member
      • Apr 2009
      • 6

      #3
      thank you! that helps a lot. :)

      Comment

      • cj007541
        New Member
        • Apr 2009
        • 6

        #4
        Originally posted by bvdet
        Third, the delete() method is a canvas method, not a method of the text ID, which is an integer.
        Ok, i did everything else and i have gotten it to where it will do the countdown with the loop and no globals and everything. Only problem im still having is i dont know how to get the text off the canvas. It stays on and when i try to delete it off the canvas it gives me that error again. Sorry but can you help me get it to where i can delete every number before the next one comes up. Thanks.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Use canvas method delete().
          Code:
                  self.text2=self.can.create_text(400, 325, text=str(i))
                  self.root.update()
                  time.sleep(1)
                  self.can.delete(self.text2)
          -BV

          Comment

          • cj007541
            New Member
            • Apr 2009
            • 6

            #6
            oh duh. lol. thanks! :)

            Comment

            Working...