473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how come .insert() don't work

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
9 1831
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
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()
Jul 18 '05 #3
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
5
by: me | last post by:
I'm also having problems getting the bulk insert to work. I don't know anything about it except what I've gleened from BOL but I'm not seeming to get anywhere...Hopefully there is some little (or...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
3
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code...
7
by: Lorenzino | last post by:
Hi, I have a problem with bindings in a formview. I have a formview; in the insert template i've created a wizard control and inside it i have an HTML table with some textboxes bound to the...
1
by: dba123 | last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method which does an insert but how can I do this Asyncronously so that it does not affect the load on our public production...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
4
by: ryushinyama | last post by:
I had to do a lot of searching to get this one to work and in doing so I saw a lot of different sites where people were looking for this answer so I thought I would put it up. If you are trying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.