python - 是否可以像在 Entry 中一样通过单击来更改 canvas.create_text 中的内容?

标签 python python-3.x tkinter tkinter-canvas tkinter-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只要。

最佳答案

canvas_textbox = canvas.create_text()将返回一个对象 ID(数字)
首先,将 Canvas 绑定(bind)到鼠标。然后将鼠标位置传给closest=canvas.find_closest(x, y) ,这将返回 x,y 位置下的项目 id。
现在检查对象 id 文本是否在 closest 中.如果是最近使用create_window放置Entry小部件位于鼠标位置或您选择的位置。
这是代码:

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()
(双击文本)

关于python - 是否可以像在 Entry 中一样通过单击来更改 canvas.create_text 中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65378005/

相关文章:

python - 在没有真实索引的情况下 reshape Pandas 数据框

python-3.x - 在 Python 3 中从同一包内和包外导入模块

python - Django 无法连接到远程 RDS

python - 如何在存在生成器/协程的情况下检查程序状态?

python - 选择在哪个桌面 Tkinter.Tk() 打开新窗口

来自另一个函数的 Python tkinter get()

python - 如何使用 PyTest 正确测试 Django API ListView?

python - 将大型收藏分成较小的收藏?

python - 在 tkinter 中,为什么 winfo_height() 总是返回 1?

python - 无法同时打开多个程序