Connecting Tech Pros Worldwide Help | Site Map

'int' object has no attribute 'delete'

Newbie
 
Join Date: Apr 2009
Posts: 6
#1: Apr 8 '09
Hey. Im working on this for a school project thing and i need some help please.

Expand|Select|Wrap|Line Numbers
  1.  
  2. from Tkinter import*
  3. import random
  4. import time
  5.  
  6. number = 5
  7. i = 0
  8.  
  9. class Test():
  10.     def __init__(self):
  11.         i=0
  12.         self.root = Tk()
  13.         self.root.title('Test')
  14.  
  15.         self.can = Canvas(self.root, width= 800, height = 600, bg='Blue')
  16.         self.can.grid()
  17.  
  18.         self.b = Button(self.root, width=25, height = 5, text="START TEST", command=self.countdown)
  19.         self.b.grid(column=0, row=0)
  20.  
  21.         self.root.mainloop()
  22.  
  23.  
  24.  
  25.  
  26.     def countdown(self):
  27.         global i
  28.         global number
  29.  
  30.         if i == 0:
  31.             self.b.destroy()
  32.  
  33.             self.text=self.can.create_text(400, 300, text='TEST STARTS IN...')
  34.             i+=1
  35.             self.countdown()
  36.         elif i>0 and i<6:
  37.             self.text=self.can.create_text(400, 325, text=number)
  38.             time.sleep(1)
  39.             self.text.delete()
  40.             self.root.update()
  41.             number=number-1
  42.             self.countdown()
  43.         else:
  44.             pass
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. Test()
  52.  
  53.  
  54.  
Thank you.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: Apr 8 '09

re: 'int' object has no attribute 'delete'


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.
Expand|Select|Wrap|Line Numbers
  1.         self.b.destroy()
  2.         for i in range(6, 0, -1):
  3.             if i == 6:
  4.                 ** do stuff **
  5.             else:
  6.                 ** do other stuff **
  7.         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
Newbie
 
Join Date: Apr 2009
Posts: 6
#3: Apr 17 '09

re: 'int' object has no attribute 'delete'


thank you! that helps a lot. :)
Newbie
 
Join Date: Apr 2009
Posts: 6
#4: May 1 '09

re: 'int' object has no attribute 'delete'


Quote:

Originally Posted by bvdet View Post

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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#5: May 1 '09

re: 'int' object has no attribute 'delete'


Use canvas method delete().
Expand|Select|Wrap|Line Numbers
  1.         self.text2=self.can.create_text(400, 325, text=str(i))
  2.         self.root.update()
  3.         time.sleep(1)
  4.         self.can.delete(self.text2)
-BV
Newbie
 
Join Date: Apr 2009
Posts: 6
#6: May 4 '09

re: 'int' object has no attribute 'delete'


oh duh. lol. thanks! :)
Reply