简体   繁体   English

如何将数据从一个Tkinter Text小部件复制到另一个?

[英]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 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. 例如,我正在尝试将text1的数据插入到text ,但它无法正常工作。

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: 您正在尝试在另一个Text小部件的末尾插入Text引用(没有多大意义),但您实际想要做的是将Text小部件的内容复制到另一个:

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 . "1.0"表示第1行第0列。 Yes, the lines are 1-indexed and the columns are 0-indexed. 是的,这些行是1索引的,列是0索引的。


Note that you may not want to import the entire Tkinter package, using from Tkinter import * . 请注意,您可能不想使用from Tkinter import *导入整个Tkinter包。 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. 您可以选择一个简短的名称(如"tk" )。 Regardless, you should stick to one import mechanism for the library. 无论如何,您应该坚持使用库的一种导入机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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