简体   繁体   中英

Swapping lines in a Tkinter text widget

I Have everything set up, I just can't figure out how to swap 2 lines in a Tkinter text widget. It's disabled and populated by other widgets so I gave the disabled/unfocused text widget some functionality with 3 buttons; Move_Up, Move_Down, and Delete. I have delete working but can't figure out how to get the other two to work. Right now i'm working with 2 values that reference the start and end of the line of text that is going to be modified: self.line_start and self.line_end

And here's what I have so far:

def Move_Up(self):
   self.TextWidg.config(state='normal')
   #swap this line with the line above it
   self.TextWidg.config(state='disabled')

def Move_Down(self):
   self.TextWidg.config(state='normal')
   #swap this line with the line below it
   self.TextWidg.config(state='disabled')

def Delete(self):
   self.TextWidg.config(state='normal')
   #delete the line
   self.TextWidg.delete(self.line_start,self.line_end)
   #delete the carriage return
   self.TextWidg.delete(self.line_start)
   self.TextWidg.config(state='disabled')

Basically, how would I implement my values of self.line_start and self.line_end to swap the line with the line before it or the line after it.

Following Bryan's suggestions, I was able to solve the Move_Up() and Move_Down() methods as follows. It works using Python 3.1.3 or 2.6.6 on Mac OS X.

#swap this line with the line above it
def Move_Up():
    text.config(state='normal')
    # get text on current and previous lines
    lineText = text.get("insert linestart", "insert lineend")
    prevLineText = text.get("insert linestart -1 line", "insert -1 line lineend")

    # delete the old lines
    text.delete("insert linestart -1 line", "insert -1 line lineend")
    text.delete("insert linestart", "insert lineend")

    # insert lines in swapped order
    text.insert("insert linestart -1 line", lineText)
    text.insert("insert linestart", prevLineText)
    #text.config(state='disabled')


#swap this line with the line below it
def Move_Down():
    text.config(state='normal')
    # get text on current and next lines
    lineText = text.get("insert linestart", "insert lineend")
    nextLineText = text.get("insert +1 line linestart", "insert +1 line lineend")

    # delete text on current and next lines
    text.delete("insert linestart", "insert lineend")
    text.delete("insert +1 line linestart", "insert +1 line lineend")

    # insert text in swapped order
    text.insert("insert linestart", nextLineText) 
    text.insert("insert linestart + 1 line", lineText)
    #text.config(state='disabled')

EDIT: Note that it if there is only one line of text Move_Up() will append that text to that line. And Move_Down() does nothing if there is only one line.

You can get the index of any position in the widget with the index method. You can give it an argument that includes modifiers such as linestart and lineend . You can also get the index of a relative position with something like +1c to get the index of the next character, or +1l to get the next line. You can also use wordstart and wordend . You can combine them, for example: index("insert lineend +1c")

For example, to get the start and end of the line that has the insertion cursor (where 'insert' is the name of the mark that represents the insertion cursor):

start = self.TextWidg("insert linestart")
end = self.TextWidg("insert lineend")

For more information, see the section titled "expressions" on the text widget page on effbot.org .

text.delete("insert linestart +0 line","insert +0 line lineend") Move_down delete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM