how come .insert() don't work

Eric Brunel eric_brunel at despammed.com
Thu Oct 28 03:54:12 EDT 2004


Bennie wrote:
> 
> Hi,
> 
> This is a chunck out of my program:
> --------------------------------------------------
> from Tkinter import *
> 
> class App:
>     def __init__(self, master):
>         frame = Frame(master)
>         frame.pack()
>        
>         self.menubar = Menu(root)
>         self.html = Menu(self.menubar, tearoff=0)
>         self.html.add_command(label="p", command=self.tekst_in('p'))

You're using your tekst_in method before setting the attribute self.tekst, so 
when the method code is executed, there is actully no attribute named tekst; 
hence the Attribute error.

Move the previous line after the self.tekst.pack(...) line and everything should 
be fine.

>     #    self.html.add_command(label="p", command=self.tekst_a)
>         self.menubar.add_cascade(label="html", menu=self.html)
>        
>         root.config(menu=self.menubar)
>        
>         self.tekst = Text()
>         self.tekst.pack(fill=BOTH, expand=YES)
>     
>     def tekst_in(self, tag):
>         tekst_tag={'p': '\t\t<p>\n\n\t\t</p>\n', 'br': '<br />'}
>         self.tekst.insert(INSERT, tekst_tag[tag])
> 
>     # This works
>     #def tekst_a(self):
>     #    self.tekst.insert(INSERT, "<p> </p>")
>        
> 
> if __name__ == '__main__':
>     root = Tk()
>     app = App(root)
>     root.mainloop()
> 
> 
> --------------------------------------------------
> 
> 
> The error is:
> -------------------------------
> Traceback (most recent call last):
>   File "test.py", line 30, in ?
>     app = App(root)
>   File "test.py", line 10, in __init__
>     self.html.add_command(label="p", command=self.tekst_in('p'))
>   File "test.py", line 21, in tekst_in
>     self.tekst.insert(INSERT, tekst_tag[tag])
> AttributeError: App instance has no attribute 'tekst'
> ------------------------------------------------------
> hope this is enough info.

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list