【发布时间】:2020-01-21 20:24:47
【问题描述】:
简单地说,我想在文本小部件中写入内容,然后将其复制到相同坐标上的画布小部件上(在下面的代码中,文本小部件的大小与画布大小相同)
这是写好的代码:
from tkinter import *
#root configuration
root = Tk()
root.config(bg='black')
#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
global text_editor
global written_text
#gets text from the text widget and destroys it
written_text = text_editor.get('0.0', '100.0')
text_editor.destroy()
#i want the copied text from the text widget to be inserted on the canvas
text_editor = Canvas(root, height=851, width=601, bg='white')
text_editor.pack()
text_editor.insert()
#button
button = Button(root, command=replace_TextWidget_with_CanvasWidget)
button.pack()
#initial text widget
text_editor = Text(root, height=50, width=75)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()
【问题讨论】:
-
您阅读过
Canvas小部件的文档吗?记录了创建文本项的方法。顺便说一句,文本小部件的第一个索引是"1.0",而不是"0.0"。 -
您也应该获取文本小部件中使用的字体,因为文本小部件和画布中使用的字体很可能不同。
-
你在哪里有
canvas.create_text(...)将文本放在画布上?见Canvas。如果你没有使用text_id = canvas.create_text(...),你就不能使用canvas.insert(text_id, ...)
标签: python-3.x canvas tkinter text widget