[英]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()
如何将文本小部件中的数据添加到另一个文本小部件?
例如,我正在尝试将text1
的数据插入到text
,但它无法正常工作。
您正在尝试在另一个Text
小部件的末尾插入Text
引用(没有多大意义),但您实际想要做的是将Text
小部件的内容复制到另一个:
def button1():
text.insert(INSERT, text1.get("1.0", "end-1c"))
在我看来,这不是一种直观的方式。 "1.0"
表示第1
行第0
列。 是的,这些行是1索引的,列是0索引的。
请注意,您可能不想使用from Tkinter import *
导入整个Tkinter
包。 它可能会导致混乱。 我建议使用:
import Tkinter
text = Tkinter.Text()
另一种选择是:
import Tkinter as tk
text = tk.Text()
您可以选择一个简短的名称(如"tk"
)。 无论如何,您应该坚持使用库的一种导入机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.