简体   繁体   中英

Keeping the Insertion Cursor on a readonly Tkinter Entry

I am using the tkinter module in my python code to create my gui for an simple app I am creating. I was wondering if it is possible to keep the insertion cursor visible even when the the state on my entry widget is set to " readonly ". I want the user to still see where the insertion cursor is in the text but not be able to edit the text directly using the keyboard.


Note:

When I say Insertion Cursor I don't mean the normal mouse cursor/pointer but the flashing bar which shows up on text which you can edit. I'm not sure what the proper name for it is... Below is an example of what I mean (the black bar in the Entry)

插入光标示例


I have tried setting the 'insertwidth' and other insertion cursor config options in the Entry widget to see if the cursor was perhapes just hidden but it did not work.

Any help is appreciated.

Thanks!

您可以阻止小部件上的所有键盘事件,而不是将条目小部件设置为只读,从而无法输入:

entry.bind("<Key>", lambda e: "break")

I will suggest you to disable the Entry and change its disabledbackground . Example:

entry.config(disabledbackground ="white",state="disabled")

So , assuming that you are using python 3 ,your code can be like:

from tkinter import *
root = Tk()
ent = Entry(root)
ent.config(disabledbackground ="white",state="disabled")
ent.place(x=0,y=0)
mainloop()

You entry will look like:

在此处输入图片说明

Edit:

As the question is edited, so I will suggest you to use bind . Unlike @fhdrsdg 's answers , you can use the bind function in a more easy way:

ent.bind("<Key>", "pass")

So , you can say that your complete code is:

from tkinter import *
root = Tk()
ent = Entry(root)
ent.bind("<Key>", "pass")
ent.pack()
mainloop()

Here is a screenshot:

在此处输入图片说明

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