【问题标题】:Python tkinter how can I copy information from a Text widget to a Canvas widget?Python tkinter 如何将信息从 Text 小部件复制到 Canvas 小部件?
【发布时间】:2020-01-21 20:24:47
【问题描述】:

简单地说,我想在文本小部件中写入内容,然后将其复制到相同坐标上的画布小部件上(在下面的代码中,文本小部件的大小与画布大小相同)

here is an image of the code

这是写好的代码:

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


【解决方案1】:

您似乎正试图显示一些仅显示的文本。如果是这种情况,请将其 state 属性设置为 更新后禁用。

text_editor.configure(state='disabled')

但是,如果您真的想用具有相同信息的画布替换它,这里是您修改后的代码。请注意,画布小部件没有任何插入方法。相反,它有一个 create_text(x,y,text='my text') 方法。

from tkinter import * #Not a good programming approach, use import tkinter as tk instead

#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
    global text_editor

    #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
    new_canvas = Canvas(root, height=200, width=400, bg='white')
    new_canvas.pack()
    root.update() #Necessary if you want to get the current coordinates of the canvas
    # print(new_canvas.winfo_x(),new_canvas.winfo_y()) #print canvas x,y pos
    new_canvas.create_text(new_canvas.winfo_x()+200,new_canvas.winfo_y(), text=written_text) #Create text at x,y coords

#root configuration
root = Tk()
root.config(bg='black')

#button
button = Button(root, width=10, height=2, text="OK", command=replace_TextWidget_with_CanvasWidget)
button.pack(padx=10, pady=10)

#initial text widget
text_editor = Text(root, height=10, width=50)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')

root.mainloop()

您可能无法将文本定位在正确的 x,y 坐标和文本换行。如果你想换行,create_text 有一个 width 属性。您可以将其指定为特定维度,例如 width=100
?

【讨论】:

    【解决方案2】:

    向画布添加文本

    text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
    

    # from tkinter import * # PEP8: `import *` is not preferred
    import tkinter as tk
    
    # --- functions ---
    
    def replace_TextWidget_with_CanvasWidget():
        global text_editor
        global written_text
    
        written_text = text_editor.get('1.0', 'end')
        text_editor.destroy()
    
        text_editor = tk.Canvas(root, height=851, width=601, bg='white')
        text_editor.pack()
        text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
        #text_editor.insert(text_id, 'end', written_text)
    
    # --- main ---
    
    root = tk.Tk()
    root.config(bg='black')
    
    button = tk.Button(root, text="OK", command=replace_TextWidget_with_CanvasWidget)
    button.pack()
    
    text_editor = tk.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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2013-10-22
      • 2021-04-30
      相关资源
      最近更新 更多