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

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 data from a text widget to another text widget?

For example, I'm trying to insert data in text1 into text , but it does not work.

+4
source share
1 answer

You are trying to insert a Text link at the end of another Text widget (doesn't make much sense), but what you really want to do is copy the contents of the Text widget to another

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

Not an intuitive way to do this, in my opinion. "1.0" means row 1 , column 0 . Yes, rows are 1-indexed, and columns are indexed 0.


Note that you may not want to import the entire Tkinter package using from Tkinter import * . This is likely to lead to confusion in the future. I would recommend using:

 import Tkinter text = Tkinter.Text() 

Another variant:

 import Tkinter as tk text = tk.Text() 

You can select a short name (for example, "tk" ) of your choice. Regardless, you must adhere to one import mechanism for the library.

+4
source

Source: https://habr.com/ru/post/1301198/


All Articles