有没有一种简单的方法可以为 Tkinter Text 小部件制作块状插入光标? [英] Is there an easy way to make a blocky insert cursor for the Tkinter Text widget?

查看:22
本文介绍了有没有一种简单的方法可以为 Tkinter Text 小部件制作块状插入光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Text 小部件的 insert cursor 似乎没有很多选项(只有宽度、边框和闪烁).为了复制命令行样式块状或下划线类型的光标,我尝试通过更改 insertwidth 选项开始,但它看起来不是像我希望的那样向右扩展宽度,而是扩展从光标中心出来:

The insert cursor of the Text widget doesn't seem to have a lot of options (just width, border, and blinking). To replicate a command-line style blocky or underscore-type cursor, I tried to start by changing the insertwidth option, but it looks like instead of expanding the width to the right, as I hoped, it expands out from the center of the cursor:

root = Tk()

text = Text(root)
text.pack()

text.insert('1.0', 'hello world')

text.config(insertwidth=40)

mainloop()

是不是我遗漏了一些简单的东西,或者这个功能会比改变一个选项更复杂吗?

Is there something simple I'm missing, or is this functionality going to be more complex than changing an option?

推荐答案

所以,也许我只是为自己做了太多工作,而且我还没有找到一种非常简单的方法来做到这一点,但这是我提出的解决方案处理这个问题,以防其他人需要做同样的事情:

So, maybe I just made too much work for myself and there's a really simple way to do this that I haven't found, but here's the solution I made to handle this, in case anyone else needs to do the same type of thing:

from Tkinter import Tk, Text
from tkFont import Font


class BlockyCursorText(Text):

    def __init__(self, parent):
        Text.__init__(self, parent, bg='black', fg='green', insertwidth=0,
                      font=Font(family='Courier', size=10))

        # initialize the cursor position and the color of the cursor
        self.cursor = '1.0'
        self.switch = 'green'

        self._blink_cursor()
        self._place_cursor()


    def _place_cursor(self):
        '''check the position of the cursor against the last known position
        every 15ms and update the cursorblock tag as needed'''

        current_index = self.index('insert')

        if self.cursor != current_index:  # if the insertcursor moved
            self.cursor = current_index   # store the new index
            self.tag_delete('cursorblock')# delete the cursorblock tag

            start = self.index('insert')  # get the start
            end = self.index('insert+1c') # and stop indices

            if start[0] != end[0]:         # this handles an index that
                self.insert(start, ' ')    # reaches the end of the line
                end = self.index('insert') # by inserting a space

            self.tag_add('cursorblock', start, end) # add the tag back in
            self.mark_set('insert', self.cursor)    # move the insertcursor

        self.after(15, self._place_cursor)

    def _blink_cursor(self):
        '''alternate the background color of the cursorblock tagged text
        every 600 milliseconds'''

        if self.switch == 'green':
            self.switch = 'black'
        else:
            self.switch = 'green'

        self.tag_config('cursorblock', background=self.switch)

        self.after(600, self._blink_cursor)

if __name__ == '__main__':

    root = Tk()

    text = BlockyCursorText(root)
    text.pack()
    text.insert('1.0', 'hello world')

    root.mainloop()

这篇关于有没有一种简单的方法可以为 Tkinter Text 小部件制作块状插入光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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