How can I disable text selection in a Python / Tk program?

How will this be done? I couldn't find it here or using google.

#Refrences
from tkinter import *

class Interface:

    def __init__(self,stage):

        topLeft = Frame(stage,bg='black')
        test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0)
        test.config(insertbackground='white', exportselection=0)
        test.grid()
        topLeft.grid(row=0,column=0)

def launch():
    window = Tk()
    lobby = Interface(window)
    window.mainloop()

launch()

      

+3


source to share


4 answers


I assume you want to prevent users from editing the input field? If so, you should use the config parameter (state = "disabled") eg.

test.config(insertbackground='white', exportselection=0, state="disabled")

      



be careful though I couldn't find a way to keep the background of your input box black. Hope it helps

0


source


When we select the text, we fire (fire) 3 events, which are ButtonPress, Motion and ButtonRelease. All 3 of these events call the event handler function. The function runs the select_range (start, end) method, which selects the selected text.

To disable this, we must co-handle the ButtonPress and Motion events and call the select_clear method on the widget.

If you handle events and call the select_clear method, it works but not correctly, the text will be highlighted, and when another event occurs, the highlight color will be cleared. This was due to the order in which the events were executed. This means that you must tell tk to handle your event after the default event handler. We can change the order of execution of events using the bindtags and bin_class methods



example:

from tkinter import *
from tkinter import ttk


def on_select(event):
    event.widget.select_clear()  # clear selected text.


root = Tk()

name_entry = ttk.Entry(root)
name_entry.pack()

# with PostEvent tag, on_select the event hanlde after default event handler
name_entry.bindtags((str(name_entry), "TEntry", "PostEvent", ".", "all"))
name_entry.bind_class("PostEvent", "<ButtonPress-1><Motion>", on_select)


root.mainloop()

      

0


source


You can set the color of the text highlighting to match the background of the input widget. Note that the text in the widget can still be selected, but the user will not see it visually, which will give the illusion that the selection is disabled.

test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0)
test.configure(selectbackground=test.cget('bg'), inactiveselectbackground=test.cget('bg'))

      

0


source


A useful solution for Tkinter not having a ton of built-in widgets (like JavaFX) is that you can easily make your own if you don't mind that this isn't quite what you want :) Here's an example of a rough edge of using a canvas for emulation of an input field that cannot be selected. All I've given is the insert functionality (and besides, I think too, if you were smart about that), but you might want more. (Another plus is that since it is a canvas element, you can do great formatting with it).

Is it correct that with a non-selectable login widget you mean a canvas with text written on it? If you want to disable text selection in all widgets, including the top-level frame, you can wrap the Button-1 event, deselect everything, and then destroy the event whenever text is selected.

from tkinter import *

class NewEntry(Canvas):

    def __init__( self, master, width, height ):
        Canvas.__init__( self, master, width=width,height=height )
        self.width = width
        self.height = height
        self.text = ''

    def insert( self, index, text ):
        self.text =''.join([self.text[i] for i in range(index)] + [text] + [self.text[i] for i in range(index,len(self.text))])
        self.delete(ALL)
        self.create_text(self.width//2,self.height//2,text=self.text)

root = Tk()
entry = NewEntry(root,100,100)
entry.insert(0,'hello world')
entry.insert(5,'world')
entry.pack()
root.mainloop()

      

-1


source







All Articles