0

我可以从输入框中一次读取一行,并将其显示在一个新框中,但我需要前进到下一行代码。

这是 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)

这些是我从一个盒子复制到另一个盒子的功能(输出将insert()进入lexicalResult()函数)

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")
4

1 回答 1

0

您可以通过在索引上使用“lineend”修饰符来读取单行文本。例如,要获取所有第 1 行,您可以使用以下内容:

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

除了尾随的换行符之外,这将使所有内容都上线。如果您想要尾随换行符,请多获取一个字符:

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

要删除整行,您可以使用完全相同的索引并将它们传递给delete方法。

作为更通用的规则,给定任何索引,要计算下一行的开头,您可以使用以下内容:

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

index方法将索引转换为其规范形式。"lineend" 修饰符将索引更改为行尾,并将+1c其移动一个字符。

于 2019-09-20T22:08:25.617 回答