Python3(Tkinter)在焦点上突出显示文本

Python3(Tkinter)在焦点上突出显示文本,tkinter,python-3.7,Tkinter,Python 3.7,我试图找出如何在我的文本小部件中突出显示焦点设置为的文本。因此,用户只需单击应用程序,键入,然后按enter键,而无需单击高亮显示即可更改文本。有人知道这是否可以做到吗 当前图形用户界面: window.geometry('375x125') window.title("VPLS Lookup Tool") version = Label(window, text='VPLS Lookup Tool v.1.1') version.grid(column=1, r

我试图找出如何在我的文本小部件中突出显示焦点设置为的文本。因此,用户只需单击应用程序,键入,然后按enter键,而无需单击高亮显示即可更改文本。有人知道这是否可以做到吗

当前图形用户界面:

    window.geometry('375x125')
    window.title("VPLS Lookup Tool")
    version = Label(window, text='VPLS Lookup Tool v.1.1')
    version.grid(column=1, row=0)
    vplsnumber = Label(window, text='VPLS Number:')
    vplsnumber.grid(column=0, row=1)
    vpls = Entry(window,width=10)
    vpls.focus_set()
    vpls.grid(column=1, row=1)
    #Placing the button and what specifying what to do when clicked
    btn = Button(window, text="Navigate to File", command=clicked)
    btn.grid(column=1, row=2)
    addpath = Button(window, text="Mount K Drive", command=clicked2)
    addpath.grid(column=1, row=3)
    window.bind('<Return>', Keyboard_Entry)
    window.bind('<KP_Enter>', Keyboard_Entry)
    window.mainloop()```
window.geometry('375x125')
窗口标题(“VPLS查找工具”)
版本=标签(窗口,text='VPLS查找工具v.1.1')
version.grid(列=1,行=0)
vplsnumber=标签(窗口,文本='VPLS编号:')
vplsnumber.grid(列=0,行=1)
vpls=入口(窗口,宽度=10)
vpls.focus_set()
vpls.grid(列=1,行=1)
#放置按钮和指定单击时要执行的操作
btn=按钮(窗口,text=“导航到文件”,命令=单击)
btn.网格(列=1,行=2)
addpath=按钮(窗口,text=“装载K驱动器”,命令=单击2)
addpath.grid(列=1,行=3)
窗口绑定(“”,键盘输入)
窗口绑定(“”,键盘输入)
window.mainloop()```

如果我理解了您要使用的问题,请使用方法
选择\u range
。这将选择准备替换、删除或复制的文本字符。它包含在一个回调函数中,用于

将tkinter作为tk导入
def在焦点上选择焦点(事件):
event.widget.select_range(0,tk.END)#选择小部件中的所有文本。
root=tk.tk()
ent=tk.Entry(根)
ent.grid()
ent.focus_set()
ent.insert(0,“abcde”)
ent.bind(“”,在焦点上选择)
root.mainloop()
当你说“文本小部件”时,你真的是指
条目
小部件吗?我在代码中没有看到您创建
文本
小部件的地方。请创建一个适当的小部件。您发布的代码至少有六个原因无法运行。
import tkinter as tk

def select_on_focus(event):
    event.widget.select_range(0, tk.END) # Select all the text in the widget.

root = tk.Tk()

ent = tk.Entry(root)
ent.grid()
ent.focus_set()
ent.insert(0, 'abcde')
ent.bind('<FocusIn>', select_on_focus)

root.mainloop()