Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy data from one Tkinter Text widget to another?

Tags:

from Tkinter import *

root = Tk()
root.title("Whois Tool")

text = Text()
text1 = Text()

text1.config(width=15, height=1)
text1.pack()

def button1():
    text.insert(END, text1)

b = Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=60, height=15)
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

root.mainloop()

How can I add the data from a text widget to another text widget?

For example, I'm trying to insert the data in text1 to text, but it is not working.

like image 295
sourD Avatar asked Feb 13 '10 15:02

sourD


1 Answers

You are trying to insert a Text reference at the end of another Text widget (does not make much sense), but what you actually want to do is to copy the contents of a Text widget to another:

def button1():
    text.insert(INSERT, text1.get("1.0", "end-1c"))

Not an intuitive way to do it in my opinion. "1.0" means line 1, column 0. Yes, the lines are 1-indexed and the columns are 0-indexed.


Note that you may not want to import the entire Tkinter package, using from Tkinter import *. It will likely lead to confusion down the road. I would recommend using:

import Tkinter
text = Tkinter.Text()

Another option is:

import Tkinter as tk
text = tk.Text()

You can choose a short name (like "tk") of your choice. Regardless, you should stick to one import mechanism for the library.

like image 92
Matthew Flaschen Avatar answered Oct 03 '22 18:10

Matthew Flaschen