0

I can read one line at a time, from an input box, and display it in a new box, but I need to advance to the next line of code.

Here are the text boxes in the GUI

    self.sourcecode = Text(master, height=8, width=30)
    self.sourcecode.grid(row=1, column=1, sticky=W)

    self.nextline = Button(master, text="Next Line", fg="orange", command=lambda:[self.nextLine(self.intcount), self.lexicalResult()])
    self.nextline.grid(row=12, column=1, sticky=E)

    self.lexicalresult = Text(master, height=8, width=30)
    self.lexicalresult.grid(row=1, column=3, sticky=W)

These are my functions to copy from one box, to the other (output would insert() into the lexicalResult() function)

def nextLine (self, intcount):
    print("Reading from source")
    self.linenumber.delete('1.0', END)
    self.intcount = self.intcount + 1
    self.linenumber.insert('0.0', self.intcount)
    self.retrieve_input()

def retrieve_input(self):
    lines = self.sourcecode.get('1.0', '2.0') #I need to take this and move to the next line but i am new to python and don't know what functions there are or their arguments
    self.lexicalresult.insert('1.0', lines)

def lexicalResult (self):
    print("Printing to result")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • What happens when you run that code? What does it do, and how is it different from what you expect? – Bryan Oakley Sep 20 '19 at 18:17
  • @BryanOakley If I enter two lines of source code into the sourcecode box, it outputs both of the lines on the same line of the result box, with just a space in between. I want it it read one line, then print one line, then repeat – Sammy Huber Sep 20 '19 at 18:26
  • Use either this [explain-tkinter-text-search-method](https://stackoverflow.com/questions/19464813/explain-tkinter-text-search-method) or get the whole `sourcecode` and do `split('\n')` – stovfl Sep 20 '19 at 19:29
  • Split worked great thanks! – Sammy Huber Sep 20 '19 at 23:29

1 Answers1

0

You can read a single line of text by using the "lineend" modifier on an index. For example, to get all of line 1 you could use something like this:

text.get("1.0", "1.0 lineend")

That will get everything on the line except the trailing newline. If you want the trailing newline, get just one character more:

text.get("1.0", "1.0 lineend+1c")

To delete an entire line you could use the very same indexes and pass them to the delete method.

As a more general purpose rule, given any index, to compute the beginning of the next line you can use something like this:

next_line = text.index("{} lineend+1c".format(index))

The index method converts an index into it's canonical form. The "lineend" modifier changes the index to the end of the line, and +1c moves it one character further.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685