是否可以像在条目中一样通过单击canvas.create_text来更改内容?



以下是我的代码,

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

canvas_textbox = canvas.create_text()将返回一个对象ID(数字)

首先,将画布绑定到鼠标。然后将鼠标位置传递给closest=canvas.find_closest(x, y),这将返回 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()

(双击文本)

我会这样做。但是您应该考虑不同的输出和控制窗口。但这里是它是如何工作的。

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

画布现在已使用"更新"按钮进行更新。并使用标签,这在 Tkinter 中是一个非常强大的东西,非常方便。如果您希望在不按更新按钮的情况下更新文本,请使用内置的之后。

相关内容

最新更新