user12315371
user12315371

Reputation:

Python tkinter how can I copy information from a Text widget to a Canvas widget?

To put it simple, i want to write stuff in the text widget and then have it copied onto a canvas widget on the same coordinates (in the code below the Text widget size is the same as the canvas one)

here is an image of the code

Here is the written 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()

Upvotes: 0

Views: 434

Answers (2)

ak_linus
ak_linus

Reputation: 562

It looks like you're trying to show some text to display only. If that's the case, set it's state property to disabled after updating it.

text_editor.configure(state='disabled')

However, if you really want to replace it with a canvas with the same information, here is your modified code. Please note that canvas widget doesn't have any insert method. Instead, it has a create_text(x,y,text='my text') method.

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

You may have trouble positioning the text at the correct x,y co-ordinates and with text wrapping. If you want text wrapping, create_text has a width property. You can specify it to a specific dimension like width=100
šŸ˜‰

Upvotes: 1

furas
furas

Reputation: 142641

To add text to Canvas

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

Upvotes: 0

Related Questions