简体   繁体   English

在 Tkinter 条目中隐藏文本 Cursor

[英]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.所以我正在使用 Tkinter 条目,我想知道是否有办法在条目中隐藏文本 cursor。 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.此条目中的文本 cursor 非常清晰可见,即使我单击屏幕上的其他位置,它也会继续闪烁。 Is there a way to manually hide the cursor in Tkinter?有没有办法手动隐藏 Tkinter 中的 cursor? 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:以下是在 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?所以这会引发同样的问题,我如何隐藏文本 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.这段代码也没有 output 我给出的图像,因为它是用 goopylib 制作的,goopylib 是我在 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:因此,对于整个代码,您可以看到https://github.com/BhavyeMathur/goopylib/blob/master/goopylib/objects/Entry.py这是我使用的程序:

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:要运行此代码,您需要安装 goopylib,您可以使用:

pip install goopylib

The cursor is visible when the widget has focus, which is important for when the user is typing into the widget.当小部件具有焦点时,cursor 可见,这对于用户在小部件中键入时很重要。

If you don't want the cursor, the documented way is to set the state to "readonly".如果您不想要 cursor,记录的方法是将 state 设置为“只读”。 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;如果该条目是只读的,那么即使输入焦点在小部件中,也不能使用小部件命令更改该值并且不会插入 cursor 将显示 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.如果您调整绑定以使您单击的内容获得焦点,则焦点将从条目小部件中移除,并且 cursor 将被隐藏,直到焦点恢复。

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:例如,如果您要在 canvas 上创建项目,则可以创建绑定以在单击时将焦点移动到 canvas:

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.当您单击 canvas 时,焦点将移至 canvas 并远离条目,因此条目将不再显示 cursor。

To hide the cursor on the entry box (known as insert cursor) we can use an argument to the entry box like:要在输入框(称为插入光标)上隐藏 cursor,我们可以使用输入框的参数,例如:

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.使用E1.focus_set()将在应用程序开始时将焦点设置到输入框,除非您单击离开。

To hide the Blinking Cursor of Entry Widget, Its Very Simple.隐藏Entry Widget的Blinking Cursor,非常简单。

Entry(root,
      insertontime=0,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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