如何从 Tkinter 画布文本项中删除 icursor? [英] How to remove icursor from Tkinter canvas text item?

查看:20
本文介绍了如何从 Tkinter 画布文本项中删除 icursor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Effbot 的 Tkinter 页面:http://effbot.org/zone/editing-canvas-text-items.htm

我遇到的问题是插入光标后,我似乎无法让它消失!

我如何停止完全编辑?

至于示例,链接页面中的那个将起作用:

# 文件:canvas-editing-example-1.py## 编辑画布项目## fredrik lundh,1998 年 12 月## fredrik@pythonware.com# http://www.pythonware.com#从 tkinter 导入 *#改成Tkinter使用python 2.x系列类 MyCanvas(框架):def __init__(self, root):Frame.__init__(self, root)self.canvas = Canvas(self)self.canvas.pack(fill=BOTH, expand=1)# 标准绑定self.canvas.bind("", self.set_focus)self.canvas.bind("", self.set_cursor)self.canvas.bind("", self.handle_key)# 向画布添加一些项目self.canvas.create_text(50, 50, text="你好")self.canvas.create_text(50, 100, text="world")定义突出显示(自我,项目):# 标记重点项目.请注意,此代码重新创建了# 每次更新的矩形,但这对于# 这个案例.bbox = self.canvas.bbox(item)self.canvas.delete("突出显示")如果 bbox:i = self.canvas.create_rectangle(bbox,填充=白色",标签=突出显示")self.canvas.lower(i, item)def has_focus(self):返回 self.canvas.focus()def has_selection(self):# hack 以解决 Tkinter 1.101 (Python 1.5.1) 中的错误返回 self.canvas.tk.call(self.canvas._w, 'select', 'item')def set_focus(self, event):如果 self.canvas.type(CURRENT) != "text":返回self.highlight(当前)# 将焦点移到项目self.canvas.focus_set() # 将焦点移到画布上self.canvas.focus(CURRENT) # 设置焦点到文本项self.canvas.select_from(CURRENT, 0)self.canvas.select_to(当前,结束)def set_cursor(self, event):# 移动插入光标item = self.has_focus()如果不是项目:返回 # 或做其他事情# 转换到画布坐标系x = self.canvas.canvasx(event.x)y = self.canvas.canvasy(event.y)self.canvas.icursor(item, "@%d,%d" % (x, y))self.canvas.select_clear()def handle_key(self, event):# 小部件范围的密钥调度程序item = self.has_focus()如果不是项目:返回插入 = self.canvas.index(item, INSERT)如果 event.char >= " ":# 可打印字符如果 self.has_selection():self.canvas.dchars(项目,SEL_FIRST,SEL_LAST)self.canvas.select_clear()self.canvas.insert(item, "insert", event.char)self.highlight(item)elif event.keysym == "BackSpace":如果 self.has_selection():self.canvas.dchars(项目,SEL_FIRST,SEL_LAST)self.canvas.select_clear()别的:如果插入 >0:self.canvas.dchars(item, insert-1, insert)self.highlight(item)# 导航elif event.keysym ==家":self.canvas.icursor(item, 0)self.canvas.select_clear()elif event.keysym ==结束":self.canvas.icursor(项目,结束)self.canvas.select_clear()elif event.keysym ==对":self.canvas.icursor(项目,插入+1)self.canvas.select_clear()elif event.keysym ==左":self.canvas.icursor(item, insert-1)self.canvas.select_clear()别的:pass # 打印 event.keysym# 尝试一下(双击文本以启用编辑)c = MyCanvas(Tk())c.pack()主循环()

双击要编辑的项目之一后,我无法使光标消失;我试过移动焦点并将索引设置为 -1,但似乎都不起作用.

解决方案

self.canvas.focus("")

<块引用>

要从项目中移除焦点,请使用空字符串调用此方法.参考

<小时>

您可以添加以下内容

self.canvas.focus_set() # 将焦点移动到画布窗口self.canvas.focus("") # 从当前拥有它的项目中移除焦点

def set_focus(self, event):如果 self.canvas.type(CURRENT) != "text":#这里返回

因此,当用户双击画布上不是由 canvas.create_text 创建的任何部分或项目时,焦点将从当前文本"项目中移除,从而停止编辑.

<小时>

另外你可以添加

self.canvas.delete("highlight")

移除焦点时移除文本周围的白色矩形.

I'm following along with Effbot's Tkinter page here: http://effbot.org/zone/editing-canvas-text-items.htm

The trouble I'm having is that after inserting the icursor, I can't seem to get it to go away!

How do I stop editing altogether?

As for examples, the one from the linked page will work:

# File: canvas-editing-example-1.py
#
# editing canvas items
#
# fredrik lundh, december 1998
#
# fredrik@pythonware.com
# http://www.pythonware.com
#

from tkinter import *
#Change to Tkinter to use python 2.x series
class MyCanvas(Frame):

    def __init__(self, root):
        Frame.__init__(self, root)

        self.canvas = Canvas(self)
        self.canvas.pack(fill=BOTH, expand=1)

        # standard bindings
        self.canvas.bind("<Double-Button-1>", self.set_focus)
        self.canvas.bind("<Button-1>", self.set_cursor)
        self.canvas.bind("<Key>", self.handle_key)

        # add a few items to the canvas
        self.canvas.create_text(50, 50, text="hello")
        self.canvas.create_text(50, 100, text="world")

    def highlight(self, item):
        # mark focused item.  note that this code recreates the
        # rectangle for each update, but that's fast enough for
        # this case.
        bbox = self.canvas.bbox(item)
        self.canvas.delete("highlight")
        if bbox:
            i = self.canvas.create_rectangle(
                bbox, fill="white",
                tag="highlight"
                )
            self.canvas.lower(i, item)

    def has_focus(self):
        return self.canvas.focus()

    def has_selection(self):
        # hack to work around bug in Tkinter 1.101 (Python 1.5.1)
        return self.canvas.tk.call(self.canvas._w, 'select', 'item')

    def set_focus(self, event):
        if self.canvas.type(CURRENT) != "text":
            return

        self.highlight(CURRENT)

        # move focus to item
        self.canvas.focus_set() # move focus to canvas
        self.canvas.focus(CURRENT) # set focus to text item
        self.canvas.select_from(CURRENT, 0)
        self.canvas.select_to(CURRENT, END)

    def set_cursor(self, event):
        # move insertion cursor
        item = self.has_focus()
        if not item:
            return # or do something else

        # translate to the canvas coordinate system
        x = self.canvas.canvasx(event.x)
        y = self.canvas.canvasy(event.y)

        self.canvas.icursor(item, "@%d,%d" % (x, y))
        self.canvas.select_clear()

    def handle_key(self, event):
        # widget-wide key dispatcher
        item = self.has_focus()
        if not item:
            return

        insert = self.canvas.index(item, INSERT)

        if event.char >= " ":
            # printable character
            if self.has_selection():
                self.canvas.dchars(item, SEL_FIRST, SEL_LAST)
                self.canvas.select_clear()
            self.canvas.insert(item, "insert", event.char)
            self.highlight(item)

        elif event.keysym == "BackSpace":
            if self.has_selection():
                self.canvas.dchars(item, SEL_FIRST, SEL_LAST)
                self.canvas.select_clear()
            else:
                if insert > 0:
                    self.canvas.dchars(item, insert-1, insert)
            self.highlight(item)

        # navigation
        elif event.keysym == "Home":
            self.canvas.icursor(item, 0)
            self.canvas.select_clear()
        elif event.keysym == "End":
            self.canvas.icursor(item, END)
            self.canvas.select_clear()
        elif event.keysym == "Right":
            self.canvas.icursor(item, insert+1)
            self.canvas.select_clear()
        elif event.keysym == "Left":
            self.canvas.icursor(item, insert-1)
            self.canvas.select_clear()

        else:
            pass # print event.keysym

# try it out (double-click on a text to enable editing)
c = MyCanvas(Tk())
c.pack()

mainloop()

After you double click on one of the items to be edited, I cannot make the cursor go away; I've tried moving focus and setting the index to -1, but neither seems to work.

解决方案

self.canvas.focus("")

To remove focus from the item, call this method with an empty string. Reference


you can add the following

self.canvas.focus_set() # move focus to canvas window
self.canvas.focus("") # remove focus from the current item that has it

To

def set_focus(self, event):
    if self.canvas.type(CURRENT) != "text":
        #Here
        return

So when the user double-clicks on any part or item of the canvas that isn't created by canvas.create_text the focus is removed from the current "text" item, hence stopping the edit.


Plus you can add

self.canvas.delete("highlight")

to remove the white rectangle around the text, when focus is removed.

这篇关于如何从 Tkinter 画布文本项中删除 icursor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆