Where is accept defined?

I dont get it, where is

accept

defined?
I have all the modules loaded from an example that uses “accept”.

accept('escape', sys.exit)

returns an error

It’s defined in DirectObject. So in order to be able to use it, you have to instantiate it:

do = DirectObject()
do.accept(...)

Or, most people prefer to make their class inherit from it:

class blah(DirectObject):
  def __init__(self):
    self.accept(...)

ShowBase also inherits from DirectObject, so this works too:

base.accept(...)

Thanks

What Im trying to do is to load only 2 lines from a text file, and when Enter is pressed

base.accept('enter', mytext.dosomething)

clear the current 2 lines

mytext.clearText

and load the next 2 lines.
The lines are generated in Panda with

mytext.setWordwrap(10)

i use this do load the text:

readme = open('txt/readme.txt')
text.setText(readme.read())

How can i do this?

You can put those calls in a function and pass that function to the ‘accept’ call.

Well Im asking how to load the next 2 lines after my current 2 lines (the lines generated with Wordwrap, not the real lines from the textfile)
sry

Do you mean loading the first two lines from a text file, or the NEXT two lines (assuming you have been reading the same file before)?

First case:

lines = file('filename', 'r').readlines[2]

Second case:

self.f = file('filename', 'r')
...
lines = []
lines.append(self.file.readline())
lines.append(self.file.readline())

Here you have to open the file ONCE, outside your event handler method, and KEEP it open all the time.

An alternate way would be to store the line number you have been reading at the last function call. Then you can reopen the file again and again.

Non-secure code, that is you have to add checks for having reached EOF, failed to provide a proper filename, close the file again, file contains only one line, etc. yourself.

I want to show the first 2 lines. If you press enter, show the next 2 lines, and like that till you reach the end of the file.
Now i found a way to save the Panda lines generated with Wordwrap as real lines:

mytext.getWordwrappedText()

looking at your code

Like I said, I want to show the first 2 lines. If you press enter, show the next 2 lines, and like that till you reach the end of the file.
Im out of ideas…

mytext = open("text.txt" ,'r')

setlines = mytext.readlines()
currentlines = 0
base.accept("enter", text.setText(setlines[currentlines:currentlines+2]))

Of course this returns an error, also I would need to write

currentlines = currentlines + 2 

if I wanted to show the next 2 lines the next time i pressed Enter , but i dont know where to put this

You found out that you have to “remember” something between two “enter” events. That’s what class members are good for. You know, the “self.” thing, and inheriting from DirectObject, what rdb has been explaining up at the start of this thread.

Something like this pseudo code:

class MyTextNode(DirectObject):
  def __init__(self):
    # load the lines within the textfile, all at once, and set the
    # lastline to the first line (index 0)
    self.lines = file('filename', 'r').readlines()
    self.lastline = 0
    self.maxIdx = len(self.line) - 1

    # create the text node and configure... TODO
    #self.text = ...
    #self.text.setWordwrap(10)...

    # register the event handler
    self.accept('enter', self.onEnter)

  def dispose(self):
    #unregister the event handler
    self.ignore('enter')

    # destroy the text node
    self.text.removeNode()

  def onEnter(self):
    # the next two lines to display
    self.lastline += 2

    idxStart = min(self.lastline, self.maxIdx)
    idxEnd = min(self.lastline+1, self.maxIdx)
    newtext = '\n'.join(self.lines[idxStart:idxEnd])

    # set the text
    self.text.setText(newtext)

Thanks
but I see bugs…

You can keep them. I don’t want them.

Okay, just tell me whats ‘\n’ for and whats it doing here?

\n is a newline character. In this case it’s used to join all items of the list together into a string, using a newline (meaning every item in the list self.lines will form its own line).

Thanks for the info.
Why the dotted syntax?

join is a function of string. This will probably make more sense to you:

newline = '\n'
newtext = newline.join(self.lines[idxStart:idxEnd])

But basically it can be written shorter by not using the ‘newline’ variable and directly calling join as method of the string.

So… ‘\n’ is a name of a class instance?

‘\n’ (or “\n”, or “”"\n""", or ‘’’\n’’’, if you prefer) is an instance of Python’s built-in str type.

http://en.wikipedia.org/wiki/Newline that’s the idea behind newlines. python-implementation , classes and isntances left aside

You can use any string methods on any string object, and anything within “” or ‘’ is a string. You might want to visit: docs.python.org/library/string.html