How to disable input to a Text widget but allow programatic input?

后端 未结 3 1471
夕颜
夕颜 2021-02-20 01:52

How would i go about locking a Text widget so that the user can only select and copy text out of it, but i would still be able to insert text into the Text

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-20 02:26

    Have you tried simply disabling the text widget?

    text_widget.configure(state="disabled")
    

    On some platforms, you also need to add a binding on <1> to give the focus to the widget, otherwise the highlighting for copy doesn't appear:

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

    If you disable the widget, to insert programatically you simply need to

    1. Change the state of the widget to NORMAL
    2. Insert the text, and then
    3. Change the state back to DISABLED

    As long as you don't call update in the middle of that then there's no way for the user to be able to enter anything interactively.

提交回复
热议问题