繁体   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()

如何将文本小部件中的数据添加到另一个文本小部件?

例如,我正在尝试将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.

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