简体   繁体   中英

Hide Text Cursor in Tkinter Entry

So I was working with Tkinter entries and I wanted to know whether or not there was a way to hide the text cursor in the entry. Here is just a sample entry I created (and it looks pretty horrible right now):

在此处输入图像描述

The text cursor in this entry is very clearly visible and it continues to blink even if I click somewhere else on the screen. Is there a way to manually hide the cursor in Tkinter? I wasn't able to find any articles on the subject so is this even possible?

Here is the code for creating an entry in tkinter:

from tkinter import *

top = Tk()

E1 = Entry(top, bd=5)
E1.pack(side=RIGHT)
E1.focus_set()

top.mainloop()

And so this will raise the same question, how do I hide the text cursor? This code also does not output the image I have given because that was made with goopylib, a graphics framework made by me on top of Tkinter. So, for the whole code you can see https://github.com/BhavyeMathur/goopylib/blob/master/goopylib/objects/Entry.py and this is the program I used:

from goopylib.imports import *

window = GraphWin("Test Window", width=110, height=110)
Entry(Point(55, 55), text_width=10).draw(window)

while True:
    update(24)

to run this code, you will need goopylib installed which you can do using:

pip install goopylib

The cursor is visible when the widget has focus, which is important for when the user is typing into the widget.

If you don't want the cursor, the documented way is to set the state to "readonly". From the canonical documentation:

If the entry is readonly, then the value may not be changed using widget commands and no insertion cursor will be displayed , even if the input focus is in the widget; the contents of the widget may still be selected.

The problem could also be simply that when you click somewhere is, that "somewhere else" wasn't designed to take keyboard focus. If you adjust your bindings so that what you click on receives focus, then focus will be removed from the entry widget and the cursor will be hidden until focus is restored.

For example, if you're creating items on a canvas you can create a binding to move the focus to the canvas when you click on it:

the_canvas.bind("<1>", lambda event: event.widget.focus_set())

When you click on the canvas, focus is moved to the canvas and away from the entry, so the entry will no longer show the cursor.

To hide the cursor on the entry box (known as insert cursor) we can use an argument to the entry box like:

Entry(top,insertontime=0,bd=5)

using E1.focus_set() will set the focus to the entry box while the app is launched at the beginning, unless you click away.

To hide the Blinking Cursor of Entry Widget, Its Very Simple.

Entry(root,
      insertontime=0,
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM