Garry
Garry

Reputation: 51

Program selected text not highlighted in Entry widget

I am trying to create a scenario where, when the user clicks on an Entry widget all the text is selected ready to be replaced. I have tried the following with no luck.

Python 3.4, tkinter 8.5, Mac OSX

# Try to work with older version of Python
from __future__ import print_function

import sys

if sys.version_info.major < 3:
    import Tkinter as tk
    from Tkinter import ttk
else:
    import tkinter as tk
    from tkinter import ttk

def EA_entry_click_event(event):
    """
    """
    EA_elev_e_1.select_range(0, tk.END)
    EA_elev_e_1.icursor(0)
    EA_elev_e_1.update_idletasks()

    return

root = tk.Tk()

root.title('Test Program')
root_f = tk.Frame(root)

selection = [[965.0, 972.0, 980.0, 988.0, 992.0, 987.0, 978.0, 981.0, 982.0, 981.0, 982.0, 987.0, 1005.0], [958.0, 965.0, 972.0, 977.0, 979.0, 972.0, 969.0, 970.0, 972.0, 973.0, 976.0, 984.0, 1004.0], [946.0, 951.0, 956.0, 959.0, 961.0, 964.0, 966.0, 967.0, 971.0, 975.0, 979.0, 989.0, 999.0], [939.0, 940.0, 946.0, 951.0, 955.0, 960.0, 963.0, 966.0, 969.0, 974.0, 979.0, 984.0, 987.0], [937.0, 939.0, 945.0, 951.0, 954.0, 957.0, 963.0, 964.0, 966.0, 969.0, 972.0, 973.0, 972.0], [929.0, 931.0, 935.0, 944.0, 951.0, 955.0, 957.0, 959.0, 961.0, 962.0, 964.0, 967.0, 973.0], [945.0, 958.0, 959.0, 947.0, 943.0, 946.0, 949.0, 950.0, 954.0, 963.0, 974.0, 982.0, 991.0], [975.0, 990.0, 987.0, 969.0, 948.0, 955.0, 961.0, 958.0, 955.0, 961.0, 969.0, 977.0, 985.0], [991.0, 998.0, 996.0, 977.0, 960.0, 958.0, 979.0, 986.0, 975.0, 964.0, 963.0, 964.0, 969.0], [991.0, 997.0, 1000.0, 991.0, 975.0, 965.0, 969.0, 992.0, 1000.0, 987.0, 987.0, 986.0, 980.0], [992.0, 995.0, 999.0, 1002.0, 997.0, 980.0, 971.0, 980.0, 1001.0, 1006.0, 1008.0, 1005.0, 996.0], [997.0, 1000.0, 1003.0, 1005.0, 1005.0, 997.0, 980.0, 978.0, 996.0, 1011.0, 1015.0, 1015.0, 1010.0], [1001.0, 1007.0, 1010.0, 1010.0, 1009.0, 1004.0, 992.0, 986.0, 995.0, 1007.0, 1014.0, 1017.0, 1022.0]]

root_f.grid()

EA_elev_e_1_var = tk.StringVar()
text1 = str(selection[0][0])
EA_elev_e_1_var.set(text1)

EA_elev_e_1 = tk.Entry(root_f,
                       textvariable=EA_elev_e_1_var,
                       width=8,
                       justify='center')

EA_elev_e_1.bind('<Button-1>', EA_entry_click_event)
EA_elev_e_1.grid()

root.geometry('+150+150')

root.mainloop()
root.quit()

I've run it through a debugger and it runs the statements.

Upvotes: 0

Views: 364

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

The problem is that your event happens before the default behavior, so all of the work you're doing is getting undone. There are several ways to solve this.

First, you can bind on <ButtonRelease-1>, which will let the default behavior happen before your binding is processed.

Second, you could prevent the default bindings from happening at all. You just need to do return "break" at the end of your callback. If you do that, you'll need to explicitly set the focus to the entry widget since that's one of the things the default binding does.

Third, a much less common solution is to adjust the bind tags so that your event happens after the default bindings rather than before.

Upvotes: 2

Related Questions