By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

'int' object has no attribute 'delete'

P: 6
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.
Apr 8 '09 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Apr 8 '09 #2

P: 6
thank you! that helps a lot. :)
Apr 17 '09 #3

P: 6
@bvdet
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.
May 1 '09 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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
May 1 '09 #5

P: 6
oh duh. lol. thanks! :)
May 4 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.