python 如何将数据从一个 Tkinter Text 小部件复制到另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2258097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy data from one Tkinter Text widget to another?
提问by sourD
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
,但它不起作用。
采纳答案by Matthew Flaschen
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
. Yes, the lines are 1-indexed and the columns are 0-indexed.
在我看来,这不是一种直观的方法。"1.0"
表示行1
,列0
。是的,行是 1-indexed,列是 0-indexed。
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:
请注意,您可能不想Tkinter
使用from Tkinter import *
. 它可能会导致混乱。我建议使用:
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. Regardless, you should stick to one import mechanism for the library.
您可以选择一个短名称(如"tk"
)。无论如何,您应该坚持使用该库的一种导入机制。