views:

23

answers:

1

I'm trying to add a feature in python that copies the entire contents of two text widgets. How would one go about that?

Pseudo Code:

    text1.SelectAll()
    C1 = text1.get(Copy)

    text2.SelectAll()
    C2 = text2.get(Copy)

    Paste('Widget 1:\n\n' + C1 + 'Widget 2:\n\n' + C2 )
+1  A: 

Just do (if you have a from Tkinter import * -- I don't like it but many use it):

C1 = text1.get(1.0, END)
C2 = text2.get(1.0, END)

Now you have the two strings. I'm not sure where that Paste is supposed to put the text into -- if you mean to replace the previous contents of text2, for example, just do

text2.delete(1.0, END)
text2.insert(END, "Whatever: %s and: %s" % (C1, C2))

To learn more about Tkinter text controls, read this chapter in effbot's online Tkinter book.

Alex Martelli
@Alex Martelli: you should get "end-1c" since Tk always adds an extra newline as the very last character in the text widget. using 1.0 and "end-1c" is the canonical way of getting all of the data in the widget.
Bryan Oakley
@Bryan, right, if you don't want the trailing newline -- good point, fixing accordingly, thanks and +1;-).
Alex Martelli