canvas.create_text里面的内容能不能像在Entry里一样通过点击来改变?

Is it possible to change the content in canvas.create_text by clicking on it just like in Entry?

以下是我的代码,

from tkinter import *
 
window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()

background = PhotoImage(file="Images/background.png") # can be any background image
canvas.create_image(300,300,image=background)

canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime")
 
window.mainloop()

是否有可能更改 canvas.create_text 以便它可以像 Entry 一样工作(当用户单击它以编辑文本时提供文本光标)但看起来像 canvas.create_text 而已。

我会那样做。但是你应该考虑不同的 windows 用于输出和控制。但这里是如何工作的。

from tkinter import *

window = Tk()
canvas = Canvas(window, width=300, height=300, bd=0)
canvas.pack()

def updatetext(): #create a function
    x = newtext.get()
    print(x)
    canvas.itemconfig(tagOrId='text', text = x)

background = PhotoImage(file="background.png")  # can be any background image
canvas.create_image(300, 300, image=background)
newtext = StringVar(value = 'Your new text')

canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag = 'text')
Entry(window, textvariable = newtext).pack()
Button(window, command = updatetext, text = 'Update text').pack()


window.mainloop()

Canvas 现在使用更新按钮更新。并使用标签,它在 Tkinter 中是一个非常强大的东西,非常方便。如果您想在不按更新按钮的情况下更新文本,请使用内置 after.

canvas_textbox = canvas.create_text() 将 return 对象 ID(数字)

首先,将canvas绑定到鼠标上。然后将鼠标位置传递给 closest=canvas.find_closest(x, y),这将 return x,y 位置下的项目 ID。

现在检查对象 ID 文本是否在 closest 中。如果距离最近,请使用 create_windowEntry 小部件放置在鼠标位置或您选择的位置。

代码如下:

from tkinter import *
from PIL import Image, ImageTk

def update(event):
    canvas.delete('entry')
    canvas.itemconfig(tagOrId='text', text=text_box.get())

def clicked(event):

    closest = canvas.find_closest(event.x, event.y)# returns the closest item to x, y in the form of tuple
    
    if 2 in closest:
        canvas.itemconfig(tagOrId='text', text='')
        canvas.create_window(event.x, event.y, window=text_box, tag='entry')
    else:
        print('No')

window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()

background = ImageTk.PhotoImage(Image.open(r"\path.jpg")) # can be any background image
canvas.create_image(300,300,image=background)

canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag='text')
text_box = Entry(window)
text_box.bind('<Return>', update)

print(canvas.find_all()) # returns all the items in canvas as tuple

canvas.bind('<Button>', clicked)

window.mainloop()

或者你也可以试试这个:

from tkinter import *
from PIL import Image, ImageTk

def update(event):
    canvas.delete('entry')
    canvas.itemconfig(tagOrId='text', text=text_box.get())

def clicked(event):

    closest = canvas.find_closest(event.x, event.y)# returns the closest item to x, y in the form of tuple
    x, y = canvas.coords(closest)
    
    if canvas_textbox in closest:
        canvas.itemconfig(tagOrId='text', text='')
        canvas.create_window(x+100, y, window=text_box, tag='entry')

    else:
        print('No')

window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()

background = ImageTk.PhotoImage(Image.open(r"\image")) # can be any background image
canvas.create_image(300,300,image=background)

canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag='text')
text_box = Entry(window, borderwidth=0, highlightthickness=0)
text_box.insert(0, 'TOUCH ME TO EDIT THIS TEXT')
print(canvas.coords(2))
text_box.bind('<Return>', update)

print(canvas.find_all()) # returns all the items in canvas as tuple

canvas.bind('<Double-1>', clicked) 

window.mainloop()

(双击文字)