如何将数据从一个 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?
例如,我试图将 text1
中的数据插入到 text
,但它不起作用.
For example, I'm trying to insert the data in text1
to text
, but it is not working.
推荐答案
您正试图在另一个 Text
小部件的末尾插入一个 Text
引用(不会使很有意义),但您实际上想要做的是将 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:
def button1():
text.insert(INSERT, text1.get("1.0", "end-1c"))
在我看来,这不是一种直观的方法."1.0"
表示行 1
,列 0
.是的,行是 1-indexed,列是 0-indexed.
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.
请注意,您可能不想使用 from Tkinter import *
导入整个 Tkinter
包.它可能会导致混乱.我建议使用:
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()
另一种选择是:
import Tkinter as tk
text = tk.Text()
您可以选择一个您喜欢的短名称(如 "tk"
).无论如何,您应该坚持使用一种库的导入机制.
You can choose a short name (like "tk"
) of your choice. Regardless, you should stick to one import mechanism for the library.
这篇关于如何将数据从一个 Tkinter Text 小部件复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!