Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

python - Is there an easy way to make a blocky insert cursor for the Tkinter Text widget?

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()

insertwidth=40

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...