为什么文本光标坐标没有正确更新? [英] Why Text cursor coordinates are not updated correctly?

查看:21
本文介绍了为什么文本光标坐标没有正确更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了为我的文本编辑器创建一个简单的行列计数器,我决定简单地使用 tkinter.Text 小部件的 index 函数.实际上,index 函数返回一个字符串,表示作为参数传递给它的坐标的行和列.

To create a simple line-column counter for my text editor, I decided to simply use the index function of the tkinter.Text widget. In fact, the index function returns a string representing the line and column of the coordinates passed as argument to it.

具体来说,我使用 cursor_pos = text.index(tkinter.INSERT) 来获取光标的索引,因为,从 effbot 网站:

Specifically, I am using cursor_pos = text.index(tkinter.INSERT) to get the index of the cursor, since, from the effbot website on tkinter.INSERT:

tkinter.INSERT 对应插入光标.

tkinter.INSERT corresponds to the insertion cursor.

问题是 tkinter.INSERT 似乎给了我最后一个光标位置,直到我用箭头移动光标(例如).

The problem is that tkinter.INSERT seems to give me the last cursor position until I move the cursor with the arrows (for example).

这是处理行数和列数的函数:

This is the function that handles the count of the lines and columns:

def on_key_pressed(self, event=None):
    """Docs """
    if self.tpane is not None:
        print(self.lines, self.columns)

        self.tpane.update()
        cursor_pos = self.tpane._tabs[self.tpane.selected()].text.index(tkinter.INSERT)
        self.lines = int(cursor_pos.split('.')[0])
        self.columns = int(cursor_pos.split('.')[1])
        print(self.lines, self.columns)
        self.line_c.config(text='Lines: ' + str(self.lines))
        self.col_c.config(text='Columns: ' + str(self.columns))

        self.update()

我不知道你能不能理解这种情况...当我第一次在编辑器上输入一个字母时,self.columns 变量不会更新为 1(保持为 0),直到我写第二个字母,它更新为 1,依此类推.但是有一个技巧可以让它在不写新信的情况下更新.写完第一个字母后,如果我用箭头移动光标,它会将 self.columns 更新为实际 1.

I don't know if you can understand the situation... When I first type a letter on the editor, the self.columns variable does not update to 1 (remains 0) until I write the second letter, where it updates to 1, and so on. But there's a trick to make it update without writing a new letter. Once written the first letter, if I move the cursor with the arrows, it updates the self.columns to actually 1.

另一个问题是当我尝试删除一个存在的字符时.例如,如果我有 3 个字符(假设我有 self.columns 到 3),然后按 deleteself.columns 更新莫名其妙地变成4,如果我尝试删除另一个字符,此时它会更新为3.

Another problem is when I try to delete a existent character. For example, if I have 3 characters (and suppose I have self.columns to 3), and I press delete, self.columns update inexplicably to 4, and if I try to remove another character, at this point, it updates to 3.

on_key_pressed 事件处理程序中的 self.lines 也存在此问题.我不确定这是否应该发生,如果是,那么我错过了什么......

This problems exists also for the self.lines in the on_key_pressed event handler. I am not sure if this is supposed to happen, and if yes, then I am missing something...

推荐答案

发生这种情况是因为您的自定义绑定在内置绑定之前触发,而实际上是内置绑定修改了小部件并更改了光标位置.

This happens because your custom binding fire before the built-in bindings, and it is the built-in bindings that actually modify the widget and change the cursor position.

您可以利用绑定标签更改顺序.有关更多信息,请参阅此问题:关于 tkinter 中的绑定标签的基本查询

You can change the order by leveraging bindtags. For more information see this question: Basic query regarding bindtags in tkinter

有关如何更改绑定标签的示例,请参阅此答案:https://stackoverflow.com/a/3513906/7432

For an example of how to change the bindtags, see this answer: https://stackoverflow.com/a/3513906/7432

如果不想处理绑定标签,可以绑定到.内置绑定发生在一个键 press 上,因此 release 绑定将始终在小部件更新后触发.

If you don't want to deal with bindtags, you can bind to <KeyRelease>. Built-in bindings happen on a key press, so the release binding will always fire after the widget has been updated.

这篇关于为什么文本光标坐标没有正确更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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