首页 文章

编程在“小部件”小部件中未突出显示

提问于
浏览
0

我正在尝试创建一个场景,当用户单击Entry小部件时,所有文本都被选中以备替换 . 我试过以下没有运气 .

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()

我通过调试器运行它并运行语句 .

1 回答

  • 2

    问题是你的事件发生在默认行为之前,所以你正在做的所有工作都要撤消 . 有几种方法可以解决这个问题 .

    首先,您可以绑定 <ButtonRelease-1> ,这将在处理绑定之前发生默认行为 .

    其次,您可以防止默认绑定发生 . 你只需要在回调结束时做 return "break" . 如果您这样做,那么'll need to explicitly set the focus to the entry widget since that'是默认绑定所做的事情之一 .

    第三,一个不太常见的解决方案是调整绑定标记,以便您的事件发生在默认绑定之后而不是之前 .

相关问题