0

I have a canvas with various images. The text entered in the text widget is displayed on the canvas. When entering, the text should move up. The entered text can be quite long.

I need to display text on canvas with different font style in its parts. i.e. some words should be displayed in normal font, some in bold or italics. I want use tag system like this. It work with text widget, but not with canvas.

An example of what I want.

In text widget: <b>This</b> word is bold.

In the canvas: This word is bold

Simplified part of my code:

import tkinter
import tkinter.scrolledtext 
    
def change_txtabs(event):
    abilities = txt_txtabs.get(1.0,tkinter.END)
    r_abilities = repr(abilities)
    if r_abilities != "'\\n'":
        main_pct.itemconfig(txtabilities_text,text=abilities)
    else:
        main_pct.itemconfig(txtabilities_text,text="")

window = tkinter.Tk()
window.geometry('930x565')

txt_txtabs = tkinter.scrolledtext.ScrolledText(window,height=30,wrap='word',undo=True)
txt_txtabs.bind("<KeyPress>", change_txtabs)
txt_txtabs.bind("<KeyRelease>", change_txtabs)
txt_txtabs.place(x=10,y=10,width=300)

main_pct = tkinter.Canvas(bg='white',height=480, width=300)
txtabilities_text = main_pct.create_text((12,432),text="",font="Arial 10",anchor=tkinter.SW,justify=tkinter.LEFT,width=280)
main_pct.place(x=350,y=10)
window.mainloop()
ToTaCe
  • 1

1 Answers1

0

I think it is difficult to solve with the create_text method. It's not exactly what you want, but I modified your code by following the instructions in this link(Explain Tkinter text search method).

import tkinter
import tkinter.scrolledtext

def change_txtabs(event):
    countVar = tkinter.StringVar()
    txtabilities_text['state'] = 'normal'
    abilities = txt_txtabs.get(1.0, tkinter.END)
    txtabilities_text.delete(1.0, tkinter.END)
    txtabilities_text.insert(1.0, abilities)
    start = '1.0'
    while 1:
        tag_start = txtabilities_text.search("<b>", start, stopindex=tkinter.END, regexp=True, count=countVar)
        tag_end = txtabilities_text.search("</b>", start, stopindex=tkinter.END, regexp=True, count=countVar)
        if not tag_start: break
        if not tag_end: break
        tag_start=tag_start + "+3c"
        txtabilities_text.tag_add('bold', tag_start, tag_end)
        txtabilities_text.tag_configure('bold', font='TkDefaultFont 9 bold')
        txtabilities_text.delete(f"{tag_start}-3c", tag_start)
        tag_end = txtabilities_text.search("</b>", start, stopindex=tkinter.END, regexp=True, count=countVar)
        txtabilities_text.delete(tag_end, f"{tag_end}+4c")
        start = tag_end
    txtabilities_text['state'] = 'disabled'

window = tkinter.Tk()
window.geometry('930x565')

txt_txtabs = tkinter.scrolledtext.ScrolledText(window, height=30, wrap='word', undo=True)
txt_txtabs.bind("<KeyPress>", change_txtabs)
txt_txtabs.bind("<KeyRelease>", change_txtabs)
txt_txtabs.place(x=10, y=10, width=300)

main_pct = tkinter.Canvas(bg='white', height=480, width=300)
txtabilities_text = tkinter.Text(main_pct, width=38, height=35, bd=0)
main_pct.create_window((12, 12), window=txtabilities_text, anchor='nw')
txtabilities_text['state']='disabled'
main_pct.place(x=350, y=10)
window.mainloop()
kimhyunju
  • 309
  • 1
  • 7
  • This would be a good solution, if not for 2 points: 1) The text will be displayed on top of the images, i.e. it should not have a background. 2) The text should appear from the bottom, i.e. for such a solution, it will be necessary to move the window with the text up after each line appears. There are no problems with lines having /n, but I don't understand how to take count the auto-transfer lines. – ToTaCe Feb 23 '23 at 10:02