有没有一种简单的方法可以为Tkinter Text小部件制作块状插入光标?

时间:2014-06-18 16:12:08

标签: python tkinter

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

root = Tk()

text = Text(root)
text.pack()

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

text.config(insertwidth=40)

mainloop()

insertwidth=40

我是否缺少一些简单的东西,或者这个功能是否比更改选项更复杂?

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:3)

您只需将-blockcursor选项设置为True

即可
try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

text = tk.Text(root, blockcursor=True)
# which is equivalent to:
# text['blockcursor'] = True or root.tk.call(text, 'configure', '-blockcursor', True)
text.pack()

root.mainloop()

答案 2 :(得分:0)

我遇到了同样的问题,发现了更简单的解决方案here

这是我自己项目中的代码:

from Tkinter import *

root = Tk()
root.geometry("500x500")
root.configure(background = '#343833')
root.resizable(False, False)

listlabel = Label(root, height = 30, width = 60, bd=5, relief="sunken", bg = "#343833")
listlabel.pack()

inputentry = Entry(root, bg = "#343833", bd = 5, fg = "#00ff00", width = 60)
inputentry.configure(insertbackground = "#00ff00")
inputentry.configure(highlightthickness = 0)
inputentry.configure(insertwidth = 5)
inputentry.pack()

name = Label(root, bg = "#343833", text = "\nBASIC interpreter\n")
name.pack()

root.mainloop()

所以我们可以简单地说

entryObject.configure(insertwidth = <int value>)