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

how come .insert() don't work

P: n/a
Hi all,

I have a little problem.

def tekst_in(self, tag):
tekst_tag={'p': '\t\t<p>\n\n\t\t</p>\n', 'br': '<br />'}
print tekst_tag[tag]
works fine in my class

But...
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])

Then I get a AttributeError...
I seached the net, can come op white a answer.
So I hope You all can!

Bennie,

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Bennie wrote:
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])

Then I get a AttributeError...
I seached the net, can come op white a answer.


What specific attribute is mentioned in the AttributeError?
That self has no attribute tekst, or that self.tekst has
no attribute insert?

If the former, you need to arrange your class so that self has
an attribute tekst (e.g. by initializing tekst in __init__).

If the latter: what kind of thing is self.tekst?

Regards,
Martin
Jul 18 '05 #2

P: n/a
Martin v. Lwis wrote:
Bennie wrote:
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])

Then I get a AttributeError...
I seached the net, can come op white a answer.

What specific attribute is mentioned in the AttributeError?
That self has no attribute tekst, or that self.tekst has
no attribute insert?

If the former, you need to arrange your class so that self has
an attribute tekst (e.g. by initializing tekst in __init__).

If the latter: what kind of thing is self.tekst?

Regards,
Martin

self.tekst is a Tkinter.Text() so it has a insert attribute, .insert()
Jul 18 '05 #3

P: n/a
Bennie <be****@rotzjes.nl> writes:
Martin v. Löwis wrote:
Bennie wrote:
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])

Then I get a AttributeError...
I seached the net, can come op white a answer.

What specific attribute is mentioned in the AttributeError?
That self has no attribute tekst, or that self.tekst has
no attribute insert?
If the former, you need to arrange your class so that self has
an attribute tekst (e.g. by initializing tekst in __init__).
If the latter: what kind of thing is self.tekst?
Regards,
Martin

self.tekst is a Tkinter.Text() so it has a insert attribute, .insert()


See, you should have mentioned that before. What you should also
mention is:

1) the _exact_ text of the traceback. Saying "I get an AttributeError"
is not nearly enough: the full message has info that
people-in-the-know know how to interpret. Copy-and-paste it.

2) the smallest (or so) of *working* code that will exhibit the error.
The snippet in your original post doesn't work. Again, copy-and-paste.

Basically, put enough info in your question that we don't have to read
your mind.

Now I'll put my mind-reading cap on...

- Are you sure that tekst_in() is a method, not a function? The
indentation suggests function, while the self parameter suggests method.

- Are you positive self.tekst is a Tkinter.Text()? Throw in some
print statements to check. 'print repr(self)' is a good one.

- tekst_tag[tag] shouldn't be the problem (as suggested by your
original post), as that would raise a KeyError, not an AttributeError.

etc.

(I'll bet once you've done what I've suggested, you'll have found the bug...)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #4

P: n/a

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'))
# 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.
Jul 18 '05 #5

P: n/a
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
Jul 18 '05 #6

P: n/a
Eric Brunel wrote:
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

But now he execute te self.tekst_in without user input.
Jul 18 '05 #7

P: n/a
Eric Brunel <er*********@despammed.com> wrote:
...
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.


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


Nope. I guess it won't be, because I suspect the OP doesn't actually
mean to insert a 'p' during the __init__ while adding a command of None.

I guess what he means is that, when that menuentry is selected, then and
only then does a 'p' get inserted. But what he's SAYING is to call the
method right then and there, not set it as a callback as I guess he
means to do.

I think that, to make everything fine, what he needs to do is, rather,
something like:

..., command=lambda:self.tekst_in('p'))

For the OP (as I think Eric knows this): 'command' must be set to a
CALLABLE, something Tkinter WILL call without arguments later when
needed; you're setting it to the RESULT of a call that you're doing
yourself, right then and there (and that result is None). Prepending a
'lambda:' (an unfortunately murky keyword) makes and binds to command a
no-arguments callable, as needed.

It is, of course, very unlikely that method 'self.tekst_in' only ever
needs to be called with that one argument, 'p', or only ever needs to be
bound that way. If you need to bind callables that will call 'p' with
each of a range of arguments, as would usually be the case, you're
better off forgetting lambda and using closures instead. Say that,
besides 'p', you also want commands 'q', 'r', 's', 't'. Then, do:

self.html = Menu(self.menubar, tearoff=0)
def make_command(x):
def callable(): self.tekst_in(x)
return callable
for x in 'pqrst':
self.html.add_command(label=x, command=make_callable(x))
Alex

Jul 18 '05 #8

P: n/a
Bennie wrote:
self.html.add_command(label="p", command=self.tekst_in('p'))


Here you are setting the command parameter to the result of the
self.tekst_in() call which is always None. Change that to

self.html.add_command(label="p", command=self.insert_para)

and add a method to your App class that takes only the self parameter:

def insert_para(self):
self.tekst_in("p")

That way insert_para() will be invoked when you click the menu command.
Alternately you can use lambda to the same effect:

self.html.add_command(label="p", command=lambda: self.tekst_in("p"))

but I think the approach outlined above is cleaner.

Peter

Jul 18 '05 #9

P: n/a
Peter Otten wrote:
Bennie wrote:

self.html.add_command(label="p", command=self.tekst_in('p'))

Here you are setting the command parameter to the result of the
self.tekst_in() call which is always None. Change that to

self.html.add_command(label="p", command=self.insert_para)

and add a method to your App class that takes only the self parameter:

def insert_para(self):
self.tekst_in("p")

That way insert_para() will be invoked when you click the menu command.
Alternately you can use lambda to the same effect:

self.html.add_command(label="p", command=lambda: self.tekst_in("p"))

but I think the approach outlined above is cleaner.

Peter

Thanks all,
Whit lambda its is working fine.
I want to make a methode that replaces the code:

def p(self):
self.tekst.insert(INSERT, "\t\t<p>\n\n\t\t</p>\n")
def pre(self):
self.tekst.insert(INSERT, "\t\t<pre>\n\n\t\t</pre>\n")
def br(self):
self.tekst.insert(INSERT, "<br />")
etc.

Now I have to type les :-)

Bye all! and many thanks ;)
Bennie,
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.