Python tkinter, как я могу скопировать информацию из текстового виджета в виджет Canvas? - PullRequest
0 голосов
/ 21 января 2020

Проще говоря, я хочу написать материал в текстовом виджете, а затем скопировать его в виджет холста с теми же координатами (в коде ниже размер виджета Текст такой же, как у холста)

вот изображение кода

Здесь написано:

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

1 Ответ

0 голосов
/ 23 января 2020

Чтобы добавить текст в 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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...