gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:23:39 26 4
gpt4 key购买 nike

以下是我的代码,

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com